流(Stream)中保存了对集合或者数组数据的操作,和集合类似,但是集合中保存的是数据。Stream不能保存数据
通过Collection对象的stream()或者parallelStream()
通过Arrays类的stream(Array[]
通过Stream接口of() iterate() generate()方法
通过 IntStream, LongStream, DoubleStream接口中的of(), range(), rangeClosed()方法
//1. 通过Collection对象的stream()或者parallelStream()
Listlist = new ArrayList<>(); Stream
stringStream = list.parallelStream();
//2.通过Arrays类的stream()方法
String[] names = {"乔峰", "虚竹", "段誉", "阿朱", "段正淳", "萧远山"};
Streamstream = Arrays.stream(names);
//3.通过Stream接口of() iterate() generate()方法
// System.out.println("-----------通过Stream接口的of()--------");
StreamintegerStream = Stream.of(10, 20, 30, 40, 50, 60); Stream
iterate = Stream.iterate(0, x -> x + 2); Stream
generate = Stream.generate(() -> new Random().nextInt(100));
//4.1. 通过 IntStream, LongStream, DoubleStream接口中的of(), range(), rangeClosed()方法
IntStream intStream = IntStream.of(10, 20, 30, 40, 50, 60, 70, 80);
一个stream调用方法变成一个新的stream
filter(): 对元素数据进行过滤
limit(): 限制数据
skip(): 跳过几个元素
distinct(): 去重
sorted() 排序
map() 把流映射到另外一组数据
parallel() (parallelStream) 获取一个并行流
public class StreamDemo2 { public static void main(String[] args) { List
list = new ArrayList<>(); list.add(new Employee("张无忌", 12000.0)); list.add(new Employee("小昭", 18000.0)); list.add(new Employee("张三丰", 32000.0)); list.add(new Employee("金花婆婆", 15000.0)); list.add(new Employee("宋青书", 8000.0)); list.add(new Employee("灭绝师太", 13000.0)); list.add(new Employee("谢逊", 27000.0)); list.add(new Employee("成昆", 12800.0)); list.add(new Employee("殷天正", 32000.0)); list.add(new Employee("殷素素", 14500.0)); list.add(new Employee("张翠山", 16900.0)); list.add(new Employee("张松溪", 14200.0)); list.add(new Employee("灭绝师太", 13000.0)); list.add(new Employee("灭绝师太", 13000.0)); list.add(new Employee("灭绝师太", 13000.0)); list.add(new Employee("灭绝师太", 13000.0)); list.add(new Employee("灭绝师太", 13000.0)); // 1. filter 过滤 过滤出工资大于15000的员工 Stream employeeStream = list.stream().filter(e -> e.getSalary() > 15000); // employeeStream.forEach(System.out::println); //2. limit 限制数据 取集合中的前两条数据 Stream limit = list.stream().limit(2); // limit.forEach(System.out::println); //3. skip 跳过几条数据 Stream skip = list.stream().skip(3); // skip.forEach(System.out::println); //4. distinct 去重 , 集合中的元素需要重写equals方法 //list.stream().distinct().forEach(System.out::println); //5.sorted 排序 //list.stream().distinct().sorted((o1,o2)-> Double.compare(o1.getSalary(),o2.getSalary())).forEach(System.out::println); //6. map 把流映射到另外一组数据, 提取集合中员工的名字 list.stream().map(e->e.getName()).forEach(System.out::println); //parallel (parallelStream) 获取一个并行流 采用多线程提高效率 list.stream().parallel().map(e->e.getName()).forEach(System.out::println); //练习: 找出所有姓张的,按照工资升序排序 // list.stream().filter(e->e.getName().startsWith("张")).sorted((o1,o2)->Double.compare(o1.getSalary(),o2.getSalary())) // .forEach(System.out::println); //练习2: 找出工资最低的张姓人员 // list.stream().filter(e->e.getName().startsWith("张")) // .sorted((o1,o2)-> Double.compare(o1.getSalary(),o2.getSalary())) // .limit(1).forEach(System.out::println); // } }
forEach() 遍历
min() 最小值
max() 最大值
count() 总数
reduce() 规约
collect() 收集
public class StreamDemo4 {
public static void main(String[] args) {
List list = new ArrayList<>();
list.add(new Employee("张无忌", 12000.0));
list.add(new Employee("小昭", 18000.0));
list.add(new Employee("张三丰", 32000.0));
list.add(new Employee("金花婆婆", 15000.0));
list.add(new Employee("宋青书", 8000.0));
list.add(new Employee("灭绝师太", 13000.0));
list.add(new Employee("谢逊", 27000.0));
list.add(new Employee("成昆", 12800.0));
list.add(new Employee("殷天正", 32000.0));
list.add(new Employee("殷素素", 14500.0));
list.add(new Employee("张翠山", 16900.0));
list.add(new Employee("张松溪", 14200.0));
//forEach()
// list.stream().filter(e-> e.getName().startsWith("张")).forEach(System.out::println);
// min() max()
//找出工资最低的张姓人员
Optional min = list.stream().filter(e -> e.getName().startsWith("张"))
.min((o1, o2) -> Double.compare(o1.getSalary(), o2.getSalary()));
System.out.println(min.get());
//找出工资最高的员工
Optional max = list.stream().max((o1, o2) -> Double.compare(o1.getSalary(), o2.getSalary()));
System.out.println(max.get());
//count() 求总数 输出员工总数
long count = list.stream().count();
System.out.println("员工总数:" + count);
System.out.println(list.size());
//reduce() 规约 输入数据后对数据产生某些影响 对数据进行深加工,在Stream中此方法主要用于数据的叠加
//求公司员工工资总和
Optional reduce = list.stream().map(e -> e.getSalary()).reduce((x, y) -> x + y);
System.out.println(reduce.get());
int[] array = {1,2,3,4,5,6,7,8,9,10};
OptionalInt reduce1 = Arrays.stream(array).reduce((x, y) -> x + y);
System.out.println(reduce1.getAsInt());
String[] arr = {"a","b","c","d","e"};
Optional reduce2 = Arrays.stream(arr).reduce((x, y) -> x + y);
System.out.println(reduce2.get());
//collect 收集
// 收集所有员工的姓名,收集到一个集合中
List collect = list.stream().map(e -> e.getName()).collect(Collectors.toList());
//collect.forEach(System.out::println);
Iterator iterator = collect.iterator();
while(iterator.hasNext()){
String str = iterator.next();
System.out.println(str);
}
}
}
之前的API存在的问题: 线程安全问题,设计混乱
本地化的日期对象
LocalDate
LocalTime
LocalDateTime
Instant 时间戳
ZXoneld 时区
DateTimeFormatter 日期格式转换
public static void main(String[] args) throws ExecutionException, InterruptedException {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
ExecutorService executorService = Executors.newFixedThreadPool(10);
Callable callable = new Callable() {
@Override
public Date call() throws Exception {
return simpleDateFormat.parse("2023-09-16 12:11:11");
}
};
List> list = new ArrayList<>();
for (int i = 0; i < 10; i++) {
Future submit = executorService.submit(callable);
list.add(submit);
}
for (Future dateFuture : list) {
System.out.println(dateFuture.get());
}
executorService.shutdown();
}
/**
* SimpleDateFormat的线程安全问题
*/
public class DateTimeFormatterDemo {
public static void main(String[] args) throws ExecutionException, InterruptedException {
//SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
DateTimeFormatter sdf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
ExecutorService pool = Executors.newFixedThreadPool(10);
Callable callable = new Callable() {
@Override
public LocalDate call() throws Exception {
return LocalDate.parse("2023-09-15");
}
};
List> list = new ArrayList<>();
for (int i = 0; i < 10; i++) {
Future submit = pool.submit(callable);
list.add(submit);
}
for (Future dateFuture : list) {
System.out.println(dateFuture.get());
}
pool.shutdown();
}
}