public static void main(String[] args) {
Person[] person= new Person[]{new Person("张三",11),new Person("李四",12)};
//创建方法
//Stream.of(person);
//集合stream
//Intermediate 操作
// map (mapToInt, flatMap 等)、 filter、 distinct、 sorted、 peek、 limit、 skip、 parallel、 sequential、 unordered
//Function<? super T, ? extends R> mapper 处理T类型数据返回R类型数据
//依旧返回一个stream 筛选
List<Person> collect2 = Stream.of(person).filter(p -> p.getAge() > 0).collect(Collectors.toList());
//去重
final Stream<Person> distinct = Stream.of(person).distinct();
//排序
Stream.of(person).sorted(Person::compareTo).collect(Collectors.toList());
//这部操作会直接改变 person 的值
System.out.println(Stream.of(person).peek(p-> p.setAge(90)).collect(Collectors.toList()));
//并行的处理
Stream.of(person).parallel();
//串行
Stream.of(person).sequential();
List<Integer> collect = Stream.of(person).map(Person::initAge).collect(Collectors.toList());
//Terminal 操作
// forEach、 forEachOrdered、 toArray、 reduce、 collect、 min、 max、 count、 anyMatch、 allMatch、 noneMatch、 findFirst、 findAny、 iterator
//循环输出
Stream.of(person).forEach(System.out::println);
//排序加输出
Stream.of(person).forEachOrdered(System.out::println);
Object[] objects = Stream.of(person).toArray();
System.out.println(Arrays.toString(objects));
//直接指定了数据类型
Person[] people = Stream.of(person).toArray(Person[]::new);
System.out.println(Arrays.toString(people));
//归纳 参数为BinaryOperator 方法定义 (acc,item) acc 为上次计算后的结果 item 是当前条目 最后返回的是Optional
//参数为BinaryOperator 继承 BiFunction 方法定义为传入两个参数 返回一个结果
//可以设置初始值
Integer init =20;
Integer red = Stream.of(person).map(Person::getAge).reduce(init, (acc, item) -> {
//做运算的
acc += item;
return acc;
});
System.out.println(red);
//创建 累加 合并
LinkedList<Object> collect1 = Stream.of(person).collect(LinkedList::new, LinkedList::add, LinkedList::addAll);
System.out.println(collect1);
//person要实现Comparable 接口
Stream.of(person).min(Person::compareTo).ifPresent(System.out::println);
Stream.of(person).max(Person::compareTo).ifPresent(System.out::println);
long count = Stream.of(person).count();
System.out.println(count);
//有一个元素匹配返回true
boolean b = Stream.of(person).anyMatch(p -> p.getAge() == 11);
System.out.println(b);
//要求所有元素都匹配
System.out.println(Stream.of(person).allMatch(p -> p.getAge() < 30));
//没有任何匹配返回true
System.out.println(Stream.of(person).noneMatch(p -> p.getAge() > 30));
//查找第一和元素 返回 Optional
Stream.of(person).findFirst().ifPresent(System.out::println);
//发现第一个元素就返回 //无返回会有异常
System.out.println(Stream.of(person).findAny().orElse(null));
//hort-circuiting 操作
// anyMatch、 allMatch、 noneMatch、 findFirst、 findAny、 limit