JDK1.8 Stream学习
@Data
public class Person {
private String name;
private int salary;
private int age;
private String sex;
private String area;
public Person(String name, int salary, String sex, String area) {
this.name = name;
this.salary = salary;
this.sex = sex;
this.area = area;
}
}
List<Person> personList = new ArrayList<Person>();
personList.add(new Person("Tom", 8900, "male", "New York"));
personList.add(new Person("Jack", 7000, "male", "Washington"));
personList.add(new Person("Lily", 7800, "female", "Washington"));
personList.add(new Person("Anni", 8200, "female", "New York"));
personList.add(new Person("Owen", 9500, "male", "New York"));
personList.add(new Person("Alisa", 7900, "female", "New York"));
List<Integer> list = Arrays.asList(7, 6, 9, 3, 8, 2, 1);
list.stream().filter(x->x>6).forEach(System.out::println);
Optional<Integer> first = list.stream().filter(x -> x > 6).findFirst();
Optional<Integer> any = list.parallelStream().filter(x -> x > 6).findAny();
System.out.println(list.stream().anyMatch(x -> x > 10));
List<Person> collect = personList.stream().filter(x -> x.getSalary() > 8000).collect(Collectors.toList());
collect.forEach(System.out::println);
List<String> listMMC = Arrays.asList("adnm", "admmt", "pot", "xbangd", "weoujgsd");
System.out.println(listMMC.stream().max(Comparator.comparing(String::length)).get());
List<Integer> listMax = Arrays.asList(7, 6, 9, 4, 11, 6);
System.out.println(listMax.stream().max(Integer::compareTo).get());
System.out.println(personList.stream().max(new Comparator<Person>() {
@Override
public int compare(Person o1, Person o2) {
return o1.getSalary() - o2.getSalary();
}
}));
System.out.println(personList.stream().max(Comparator.comparingInt(Person::getSalary)));
System.out.println(listMax.stream().filter(x -> x > 6).count());
String[] strArr = { "abcd", "bcdd", "defde", "fTr" };
Arrays.asList(strArr).stream().map(String::toUpperCase).collect(Collectors.toList()).forEach(System.out::println);
List<Integer> intList = Arrays.asList(1, 3, 5, 7, 9, 11);
intList.stream().map(x->x+3).collect(Collectors.toList()).forEach(System.out::println);
List<Person> collectNew1 = personList.stream().map(person -> {
Person personNew = new Person(person.getName(), 0, person.getSex(), person.getArea());
personNew.setSalary(person.getSalary() + 1000);
return personNew;
}).collect(Collectors.toList());
collectNew1.forEach(System.out::println);
List<Person> collectNew2 = personList.stream().map(person -> {
person.setSalary(person.getSalary() + 1000);
return person;
}).collect(Collectors.toList());
collectNew2.forEach(System.out::println);
List<String> listNum = Arrays.asList("m,k,l,a", "1,3,5,7");
listNum.stream().flatMap(s->{
String[] split = s.split(",");
Stream<String> stream = Arrays.stream(split);
return stream;
}).collect(Collectors.toList()).forEach(System.out::println);
List<Integer> listN = Arrays.asList(1, 3, 2, 8, 11, 4);
System.out.println(listN.stream().reduce((x, y) -> x + y));
System.out.println(listN.stream().reduce(Integer::sum));
System.out.println(listN.stream().reduce(0, Integer::sum));
System.out.println(listN.stream().reduce((s, y) -> s * y));
System.out.println(listN.stream().reduce((x, y) -> x > y ? x : y));
System.out.println(listN.stream().reduce(1, Integer::max));
System.out.println(personList.stream().map(Person::getSalary).reduce(Integer::sum));
System.out.println(personList.stream().reduce(0, (x, y) -> x += y.getSalary(), (sum1, sum2) -> sum1 + sum2));
System.out.println(personList.stream().reduce(0, (x, y) -> x += y.getSalary(), Integer::sum));
System.out.println(personList.stream().reduce(0, (x, y) -> x > y.getSalary() ? x : y.getSalary(), Integer::max));
System.out.println(personList.stream().reduce(0, (max, p) -> max > p.getSalary() ? max : p.getSalary(), (max1, max2) -> max1 > max2 ? max1 : max2));
Map<?, Person> map = personList.stream().filter(p -> p.getSalary() > 10000)
.collect(Collectors.toMap(Person::getName, p -> p));
System.out.println(map);
System.out.println(personList.stream().collect(Collectors.counting()));
System.out.println(personList.stream().collect(Collectors.averagingDouble(Person::getSalary)));
System.out.println(personList.stream().map(Person::getSalary).collect(Collectors.maxBy(Integer::compare)));
System.out.println(personList.stream().collect(Collectors.summingInt(Person::getSalary)));
System.out.println(personList.stream().collect(Collectors.summarizingDouble(Person::getSalary)));
System.out.println(personList.stream().collect(Collectors.partitioningBy(x -> x.getSalary() > 8000)));
System.out.println(personList.stream().collect(Collectors.groupingBy(Person::getSex)));
System.out.println(personList.stream().collect(Collectors.groupingBy(Person::getSex, Collectors.groupingBy(Person::getArea))));
System.out.println(personList.stream().map(p -> p.getName()).collect(Collectors.joining(",")));
System.out.println(Arrays.asList("A", "B", "C").stream().collect(Collectors.joining("-")));
System.out.println(personList.stream().collect(Collectors.reducing(0, Person::getSalary, (i, j) -> (i + j - 5000))));
System.out.println(personList.stream().map(Person::getSalary).reduce(Integer::sum));
System.out.println(personList.stream().sorted(Comparator.comparingInt(Person::getSalary)).map(Person::getName).collect(Collectors.toList()));
System.out.println(personList.stream().sorted(Comparator.comparing(Person::getSalary).reversed()).map(Person::getName).collect(Collectors.toList()));
System.out.println(personList.stream().sorted((p1, p2) -> {
if (p1.getSalary() == p2.getSalary()) {
return p2.getAge() - p1.getAge();
} else {
return p2.getSalary() - p1.getSalary();
}
}).map(Person::getName).collect(Collectors.toList()));
String[] arr1 = { "a", "b", "c", "d" };
String[] arr2 = { "d", "e", "f", "g" };
Stream<String> stream1 = Stream.of(arr1);
Stream<String> stream2 = Stream.of(arr2);
List<String> newList = Stream.concat(stream1, stream2).distinct().collect(Collectors.toList());
List<Integer> collect1 = Stream.iterate(1, x -> x + 2).limit(10).collect(Collectors.toList());
List<Integer> collect2 = Stream.iterate(1, x -> x + 2).skip(1).limit(5).collect(Collectors.toList());
System.out.println("流合并:" + newList);
System.out.println("limit:" + collect1);
System.out.println("skip:" + collect2);