1. foreach 遍历
// void forEach(Consumer<? super T> action);
personList.stream()
.forEach(person -> System.out.println("person = " + person));
2. count 统计流中元素个数
//long count();
long count = personList.stream()
.count();
System.out.println("count = " + count);
3. findFirst 查找第一个
// Optional<T> findFirst();
// 找出年龄大于18的第一个人
Optional<Person> personFirst = personList.stream()
.filter(person -> person.getAge() > 18)
.sorted(Comparator.comparing(Person::getAge))
.findFirst();
System.out.println("personFirst.get() = " + personFirst.get());
4. findAny 随机查找一个
// Optional<T> findAny();
// 提供findAny方法是为了更好的利用并行流
// 找出年龄大于60的任意一个人
Optional<Person> any = personList.parallelStream()
.filter(person -> person.getAge() > 60)
.findAny();
// 如果any不为空,则执行括号内的语句
any.ifPresent(person -> System.out.println("person = " + person));
// 判断any是不是空
if (any.isPresent()){
System.out.println("any.get() = " + any.get());
}
5. reduce 将流中的元素组合
// Optional<T> reduce(BinaryOperator<T> accumulator);
// T reduce(T identity, BinaryOperator<T> accumulator);
// reduce接受两个参数,一个初始值这里是0,一个 BinaryOperatoraccumulator,来将两个元素结合起来产生一个新值,另外reduce方法还有一个没有初始化值的重载方法。
// 求和
Optional<Integer> reduce = personList.stream()
.map(person -> person.getAge())
.reduce((age, age2) -> age + age2);
Optional<Integer> reduce1 = personList.stream()
.map(person -> person.getAge())
.reduce(Integer::sum);
Integer reduce2 = personList.stream()
.map(Person::getAge)
.reduce(0, Integer::sum);
// 求最值
// 找出年龄最大的职员,如果年龄相同,则找出工资最高的
Optional<Person> reduce3 = personList.stream()
.reduce((person, person2) -> {
int compareFlag = person.getAge().compareTo(person2.getAge());
if (compareFlag == 0){
return person.getSalary().compareTo(person2.getSalary())>0?person:person2;
}
return compareFlag > 0 ? person : person2;
});
// 求平均
// 计算平均薪资
long count1 = personList.stream().count();
double sum = personList.stream()
.mapToDouble(Person::getSalary)
.reduce(0.0, Double::sum);
System.out.println(String.format("平均薪资为:%1.2f",sum/count1));
6. min/max 获取最小最大值
// 方法参数为 Comparator<?superT>comparator
// 找出符合判断条件的最大最小数据
Optional<Person> personMax = personList.stream()
.max((o1, o2) -> {
if (o1.getAge().compareTo(o2.getAge()) == 0) {
return o1.getSalary().compareTo(o2.getSalary());
}
return o1.getAge().compareTo(o2.getAge());
});
if (personMax.isPresent()){
System.out.println("personMax.get() = " + personMax.get());
}
Optional<Person> personMin = personList.stream()
.min((o1, o2) -> {
if (o1.getAge().compareTo(o2.getAge()) == 0) {
return o1.getSalary().compareTo(o2.getSalary());
}
return o1.getAge().compareTo(o2.getAge());
});
if (personMin.isPresent()){
System.out.println("personMin.get() = " + personMin.get());
}
// 输出
// person.get() = StreamTest.Person(name=test08, age=38, salary=4.8)
// person.get() = StreamTest.Person(name=test03, age=16, salary=0.5)
7. sum / summingxxx 求和
int sumAge = personList.stream()
.mapToInt(Person::getAge)
.sum();
System.out.println("sumAge = " + sumAge);
// 如果数据类型为double、long,则通过summingDouble、summingLong方法进行求和。
Double sumSalary = personList.stream()
.collect(Collectors.summingDouble(Person::getSalary));
System.out.println("sumSalary = " + sumSalary);
// 输出
// sumAge = 262
// sumSalary = 18.8
8. averagingxxx 求平均值
Double averSalary = personList.stream()
.collect(Collectors.averagingDouble(Person::getSalary));
System.out.println(String.format("averSalary = %3.2f",averSalary));
// 输出
// averSalary = 1.88
9. summarizingxxx 同时求总和、平均值、最大值、最小值
DoubleSummaryStatistics summaryStatistics = personList.stream()
.collect(Collectors.summarizingDouble(Person::getSalary));
System.out.println("平均值:summaryStatistics.getAverage() = " + summaryStatistics.getAverage());
System.out.println("最大值:summaryStatistics.getMax() = " + summaryStatistics.getMax());
System.out.println("最小值:summaryStatistics.getMin() = " + summaryStatistics.getMin());
System.out.println("总和:summaryStatistics.getSum() = " + summaryStatistics.getSum());
System.out.println("计数:summaryStatistics.getCount() = " + summaryStatistics.getCount());
// 输出
//平均值:summaryStatistics.getAverage() = 1.8800000000000001
//最大值:summaryStatistics.getMax() = 4.8
//最小值:summaryStatistics.getMin() = 0.5
//总和:summaryStatistics.getSum() = 18.8
//计数:summaryStatistics.getCount() = 10
10. collect 返回集合
Set<Double> salarySet = personList.stream()
.map(Person::getSalary)
.collect(Collectors.toSet());
System.out.println("salarySet = " + salarySet);
List<Person> personList18 = personList.stream()
.filter(person -> person.getAge() > 18)
.collect(Collectors.toList());
System.out.println("personList18 = " + personList18);
// (a,b)->b,如果重复选择后者
Map<String, Person> personMap = personList.stream()
.collect(Collectors.toMap(Person::getName, Function.identity(),(a,b)->b));
System.out.println("personMap = " + personMap);
// 根据某个参数对列表进行去重
List<ttt> list = new ArrayList<ttt>() {{
add(new ttt().setName("1").setAge(120));
add(new ttt().setName("2").setAge(180));
add(new ttt().setName("2").setAge(18));
add(new ttt().setName("1").setAge(12));
}};
System.out.println("list = " + list);
System.out.println("***********************************************");
ArrayList<ttt> arrayList = list.stream().collect(
Collectors.collectingAndThen(
Collectors.toCollection(
() -> new TreeSet<>(Comparator.comparing(ttt::getName))
), ArrayList::new
)
);
System.out.println("arrayList = " + arrayList);
System.out.println("=================");
11. joining 拼接流中的元素
// 去除重复的姓名
String join = personList.stream()
.map(Person::getName)
.distinct()
.collect(Collectors.joining("-"));
System.out.println("join = " + join);
// 输出:join = test01-test02-test03-test04-test05-test06-test07-test08
12. groupingBy 分组
// 根据name分组
Map<String, List<Person>> groupName = personList.stream()
.collect(Collectors.groupingBy(Person::getName));
System.out.println("groupName = " + groupName);
// 根据name和age分组
Map<String, List<Person>> groupNameAge= personList.stream()
.collect(Collectors.groupingBy(person->person.getName()+"-"+person.getAge()));
System.out.println("groupNameAge = " + groupNameAge);
// 将成年的按照年龄分组,未成年的分成一组
Map<String, List<Person>> groupAge = personList.stream()
.collect(Collectors.groupingBy(person -> {
if (person.getAge() < 18) {
return "未成年";
} else {
return person.getAge() + "岁";
}
}));
System.out.println("groupAge = " + groupAge);
13. partitioningBy 分区
// 分区是特殊的分组,它分类依据是true和false,所以返回的结果最多可以分为两组。
Map<Boolean, List<Person>> listMap = personList.stream()
.collect(Collectors.partitioningBy(person -> person.getAge() > 18));
System.out.println("listMap = " + listMap);
// 将person划分为是否大于18
14.allMatch/anyMatch/noneMatch 匹配
// 是否全部person的age都大于18
boolean matchAll = personList.stream()
.allMatch(person -> person.getAge() > 18);
System.out.println("matchAll = " + matchAll);
// 是否存在person的age大于18
boolean anyMatch = personList.stream()
.anyMatch(person -> person.getAge() > 18);
System.out.println("anyMatch = " + anyMatch);
// 是否不存在person的age大于100
boolean noneMatch = personList.stream()
.noneMatch(person -> person.getAge() > 100);
System.out.println("noneMatch = " + noneMatch);
// 输出
// matchAll = false
// anyMatch = true
// noneMatch = true
标签:stream,personList,System,学习,person,println,out
From: https://blog.51cto.com/u_16155131/6901502