话不多说直接上代码
先建一个实体类
class Person {
private String name;
private int salary;
private int age;
private String sex;
private String area;
public Person(String name, int salary, int age, String sex, String area) {
this.name = name;
this.salary = salary;
this.age = age;
this.sex = sex;
this.area = area;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getArea() {
return area;
}
public void setArea(String area) {
this.area = area;
}
}
下面是所有的stream流相关的操作
List<Person> personList = new ArrayList<Person>();
personList.add(new Person("Tom",8900,18,"male","New York"));
personList.add(new Person("Jack",7000,19,"male","Washington"));
personList.add(new Person("Lily",7800,20,"female","Washington"));
personList.add(new Person("Anni",8200,30,"female","New York"));
personList.add(new Person("Owen",9500,18,"male","New York"));
personList.add(new Person("Alisa",7900,23,"female","New York"));
personList.parallelStream().filter(s -> s.getAge() > 19).forEach(System.out::println);
Optional<Person> optionalPerson = personList.stream().filter(s -> s.getAge() > 20).findFirst();
Optional<Person> optionalPerson1 = personList.parallelStream().filter(s -> s.getAge() > 21).findAny();
Stream<Person> stream = personList.stream();
Stream<Person> stream1 = personList.parallelStream();
Stream<Person> stream2 = personList.stream().parallel();
personList.stream().forEach(System.out::println);
Optional<Person> maxOptional = personList.stream().max(Comparator.comparing(Person::getSalary));
System.out.println("工资最高的人员" + JsonUtil.toJson(maxOptional));
long count = personList.stream().filter(e->e.getSalary()>8000).count();
System.out.println("计算工资大于8000的员工个数" + count);
List<Integer> personList1 = personList.stream().distinct().map(Person::getAge).collect(Collectors.toList());
System.out.println("去除元素中重复的值" + JsonUtil.toJson(personList1));
List<String> engNameList = personList.stream().filter(e -> e.getSalary() > 8000).map(Person::getName).collect(Collectors.toList());
System.out.println("员工工资高于8000的人员名字列表" + JsonUtil.toJson(engNameList));
List<String> sexList = personList.stream().map(e -> e.getSex().toUpperCase()).collect(Collectors.toList());
System.out.println("把sex中英文字符串数组的元素全部改为大写" + JsonUtil.toJson(sexList));
List<Integer> ageList = personList.stream().map(e->e.getAge()+3).collect(Collectors.toList());
System.out.println("年龄整都整体+3岁。" + JsonUtil.toJson(ageList));
Map<String, Person> map = personList.stream().collect(Collectors.toMap(Person::getName, person -> person));
System.out.println("list转map的结果" + JsonUtil.toJson(map));
Set<Person> peopleSet = personList.stream().collect(Collectors.toSet());
Map<String, Person> maps = personList.stream().filter(p -> p.getSalary() > 8000).collect(Collectors.toMap(Person::getName, p -> p));
System.out.println("list条件筛选工资大于8000的人生成新的map" + JsonUtil.toJson(maps));
List<String> nameWorkList = personList.stream().map(e -> e.getName() + e.getArea()).collect(Collectors.toList());
System.out.println("将名字和工作地合并成一起生成新的字符串" + JsonUtil.toJson(nameWorkList));
Map<Boolean, List<Person>> map1 = personList.stream().collect(Collectors.partitioningBy(e -> e.getSalary() > 8000));
Map<String, List<Person>> map2 = personList.stream().collect(Collectors.groupingBy(Person::getSex));
Map<String, Map<String, List<Person>>> group2 = personList.stream().collect(Collectors.groupingBy(Person::getSex, Collectors.groupingBy(Person::getArea)));
String engName = personList.stream().map(Person::getName).collect(Collectors.joining(","));
System.out.println("所有的名字拼接在一起生成新的字符串" + engName);
List<Person> sortList = personList.stream().sorted(Comparator.comparing(Person::getSalary)).collect(Collectors.toList());
List<Person> sortList1 = personList.stream().sorted(Comparator.comparing(Person::getSalary).reversed()).collect(Collectors.toList());
Long count1 = personList.stream().collect(Collectors.counting());
Double average = personList.stream().collect(Collectors.averagingDouble(Person::getSalary));
Optional<Integer> max = personList.stream().map(Person::getSalary).collect(Collectors.maxBy(Integer::compare));
Integer sum = personList.stream().collect(Collectors.summingInt(Person::getSalary));
DoubleSummaryStatistics collect = personList.stream().collect(Collectors.summarizingDouble(Person::getSalary));
List<List<Person>> personLists = new ArrayList<List<Person>>();
personLists.add(personList);
List<Person> = personLists.stream()
.flatMap(person-> person.stream())
.collect(Collectors.toList());