java8 Stream2
new Thread(() -> System.out.println("lambda表达式,取代匿名函数......")).start();
Stream.of("1", "1", "3").distinct().forEach(System.out::println);
System.out.println("stream --max..........");
System.out.println(Stream.of("1", "1", "3").max(Comparator.comparing(x -> x)).get().toString());
System.out.println("stream --min..........");
System.out.println(Stream.of("1", "1", "3").min(Comparator.comparing(x -> x)).get().toString());
List<Teacher> tonelist=new ArrayList<>();
tonelist.add(new Teacher("金",24));
tonelist.add(new Teacher("旭",12));
tonelist.add(new Teacher("东",25));
tonelist.add(new Teacher("张",26));
tonelist.add(new Teacher("三",22));
tonelist.stream().filter(x ->x.getAge()>20).forEach(System.out::println);
tonelist.stream().map(t ->t.getName()).forEach(System.out::println);
List<Teacher> ttwolist=new ArrayList<>();
ttwolist.add(new Teacher("A",24));
ttwolist.add(new Teacher("B",12));
ttwolist.add(new Teacher("C",25));
ttwolist.add(new Teacher("D",26));
ttwolist.add(new Teacher("E",22));
//Stream.of flatMap练习 吧多个list压扁
Stream.of(tonelist,ttwolist).flatMap(t ->t.stream()).filter(t ->t.getAge()>20).forEach(x ->System.out.println(x.getName()));
List<Teacher> list = Stream.of(tonelist,ttwolist).flatMap(x ->x.stream().filter(y ->y.getAge()>23)).collect(Collectors.toList());
for (Teacher teacher : list) {
System.out.println(teacher.toString());
}
class Teacher {
@Override
public String toString() {
return "Teacher [name=" + name + ", age=" + age + "]";
}
private String name;
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Teacher() {
super();
}
public Teacher(String name, Integer age) {
super();
this.name = name;
this.age = age;
}
}