1、java1.8的集合/数组的Stream操作和函数式编程。
2、Stream(流)创建。
a、集合.stream()。
b、Arrays.stream(数组)
c、Stream.of(元素1,元素2。。。元素n)
3、流操作代码:
实体类 SeniorStudent
@Data
@AllArgsConstructor
@NoArgsConstructor
@Component
@ApiModel(description = "学生信息")
public class SeniorStudent {
private String name;
private Integer age;
private Integer mathScore;
private Integer chineseScore;
}
List<SeniorStudent> studentList = new ArrayList();
studentList.add(new SeniorStudent("lilei",18,89,67));
studentList.add(new SeniorStudent("hanmeimei",20,34,98));
studentList.add(new SeniorStudent("zhangsan",18,90,29));
studentList.add(new SeniorStudent("lisi",22,76,87));
// 将对象的一个字段做key,然后对象值作为value,组装成一个Map
Map<String, SeniorStudent> collectMap = studentList.stream().collect(
Collectors.toMap(SeniorStudent::getName, s -> new SeniorStudent() {{
setName(s.getName());
setAge(s.getAge());
setChineseScore(s.getChineseScore());
setMathScore(s.getMathScore());
}}));
System.out.println(collectMap);
// filter 过滤器返回 Stream 然后准换成 SeniorStudent 集合
System.out.println(studentList.stream().filter(s -> s.getMathScore() > 85).collect(Collectors.toList()));
System.out.println(studentList.stream().filter(s -> s.getMathScore() > 85 && s.getChineseScore() < 60).collect(Collectors.toList()));
System.out.println(studentList.stream().filter(s -> s.getMathScore() + s.getChineseScore() > 160).collect(Collectors.toSet()));
// count 统计数量
System.out.println(studentList.stream().count());
// limit 取前几个值
studentList.stream().limit(2).forEach(System.out::println);
// skip 跳过前几个
studentList.stream().skip(2).forEach(System.out::println);
// distinct 去重
studentList.add(new SeniorStudent("lisi",22,76,87));
System.out.println(studentList.stream().count());
studentList.stream().distinct().forEach(System.out::println);
// map 映射属性,可以对每个属性操作
studentList.stream().map(s -> {
s.setAge(s.getAge()+1);
return s;
}).forEach(System.out::println);
System.out.println();
// sort 正序
studentList.stream().sorted( (a,b) -> {
return a.getChineseScore().compareTo(b.getChineseScore());
}).forEach(System.out::println);
System.out.println();
// sort 反序
studentList.stream().sorted( (a,b) -> {
return b.getChineseScore().compareTo(a.getChineseScore());
}).forEach(System.out::println);
System.out.println();
// 获取第一个
System.out.println(studentList.stream().findFirst().get());
System.out.println();
// stream 的 findAny方法获取的是第一条,stream的paralle的findAny是随机获取一个
System.out.println(studentList.stream().findAny().get());
System.out.println(studentList.stream().parallel().findAny().get());
System.out.println();
// anyMatch 任何一个元素符合条件,allMatch 所有元素都符合条件,noneMatch 没有元素符合条件
System.out.println(studentList.stream().anyMatch(s -> s.getName().equals("lilei")));
System.out.println(studentList.stream().allMatch(s -> s.getName().equals("lilei")));
System.out.println(studentList.stream().noneMatch(s -> s.getName().equals("lilei")));
System.out.println();
// max min 元素中某个属性最大和最小的
System.out.println(studentList.stream().max((a,b) -> a.getChineseScore().compareTo(b.getChineseScore())).get());
System.out.println(studentList.stream().min((a,b) -> a.getChineseScore().compareTo(b.getChineseScore())).get());
System.out.println(studentList.stream().max((a,b) -> b.getChineseScore().compareTo(a.getChineseScore())).get());
System.out.println();
// 对集合中某个元素的属性进行统计
System.out.println(studentList.stream().collect(Collectors.summarizingInt(SeniorStudent::getChineseScore)));
System.out.println(studentList.stream().collect(Collectors.summarizingInt(SeniorStudent::getChineseScore)).getCount());
System.out.println(studentList.stream().collect(Collectors.summarizingInt(SeniorStudent::getChineseScore)).getSum());
System.out.println(studentList.stream().collect(Collectors.summarizingInt(SeniorStudent::getChineseScore)).getMax());
System.out.println(studentList.stream().collect(Collectors.summarizingInt(SeniorStudent::getChineseScore)).getMin());
System.out.println(studentList.stream().collect(Collectors.summarizingInt(SeniorStudent::getChineseScore)).getAverage());
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/89022.html