List对象类(StudentInfo)
@Data
@Builder
@AllArgsConstructor
@RequiredArgsConstructor
public class StudentInfo implements Comparable<StudentInfo> {
//名称
private String name;
//性别 true男 false女
private Boolean gender;
//年龄
private Integer age;
//身高
private Double height;
//出生日期
private LocalDate birthday;
}
测试数据
//测试数据,请不要纠结数据的严谨性
List<StudentInfo> studentList = new ArrayList<>();
studentList.add(new StudentInfo("李小明",true,18,1.76,LocalDate.of(2001,3,23)));
studentList.add(new StudentInfo("张小丽",false,18,1.61,LocalDate.of(2001,6,3)));
studentList.add(new StudentInfo("王大朋",true,19,1.82,LocalDate.of(2000,3,11)));
studentList.add(new StudentInfo("陈小跑",false,17,1.67,LocalDate.of(2002,10,18)));
提取某一列(以name为例)
//从对象列表中提取一列(以name为例)
List<String> nameList = studentList.stream().map(StudentInfo::getName).collect(Collectors.toList());
提取age列并去重(使用distinct()函数)
//从对象列表中提取age并排重
List<Integer> ageList = studentList.stream().map(StudentInfo::getAge).distinct().collect(Collectors.toList());
filter()过滤List对象(查找符合条件的对象集合)
//查找身高在1.8米及以上的男生
List<StudentInfo> boys = studentList.stream().filter(s->s.getGender() && s.getHeight() >= 1.8).collect(Collectors.toList());
sorted()对List集合进行排序
集合对像定义
集合对象以学生类(StudentInfo)为例,有学生的基本信息,包括:姓名,性别,年龄,身高,生日几项。
使用stream().sorted()进行排序,需要该类实现 Comparable 接口,该接口只有一个方法需要实现,如下:
public int compareTo(T o);
排序:
//按年龄排序(Integer类型):使用年龄进行升序排序
List<StudentInfo> studentsSortName = studentList.stream().sorted(Comparator.comparing(StudentInfo::getAge)).collect(Collectors.toList());
//按年龄排序(Integer类型):使用年龄进行降序排序(使用reversed()方法)
List<StudentInfo> studentsSortName = studentList.stream().sorted(Comparator.comparing(StudentInfo::getAge).reversed()).collect(Collectors.toList());
//按年龄排序(Integer类型): 使用年龄进行降序排序,年龄相同再使用身高升序排序
List<StudentInfo> studentsSortName = studentList.stream()
.sorted(Comparator.comparing(StudentInfo::getAge).reversed().thenComparing(StudentInfo::getHeight))
.collect(Collectors.toList());
Java8利用stream lambda查询map中最大value及对应key
测试数据
Map<String,Integer> map = new HashMap<>();
map.put("1", 1);
map.put("2", 2);
map.put("3", 3);
map.put("4", 4);
map.put("5", 5);
方法
//第一种方法
Optional<Map.Entry<String, Integer>> max0 = map.entrySet().stream().max(Map.Entry.comparingByValue());
//第二种方法
Optional<Map.Entry<String, Integer>> max1 = map.entrySet().stream().max((x1, x2) -> Integer.compare(x1.getValue(), x2.getValue()));
//第三种方法
Optional<Map.Entry<String, Integer>> max3 = map.entrySet().stream().collect(Collectors.maxBy(Map.Entry.comparingByValue()));
//第四种方法
Optional<Map.Entry<String, Integer>> max4 = map.entrySet().stream().max(Comparator.comparingInt(Map.Entry::getValue));
//第五种方法
IntSummaryStatistics max5 = map.entrySet().stream().collect(Collectors.summarizingInt(Map.Entry::getValue));
list long Double 泛型 求和
//List–long求和
List<Long> longList = Arrays.asList(1L, 2L, 3L, 4L);
Long longSum = longList.stream().mapToLong(Long::longValue).sum();
System.out.println(longSum);
//使用reduce
long longSumReduce = longList.stream().reduce(Long::sum).orElse(0L);
System.out.println(longSumReduce);
//list—Double求和
List<Double> doubleList = Arrays.asList(1.0, 2.0, 3.0, 14.0);
Double doubleSum = doubleList.stream().mapToDouble(Double::doubleValue).sum();
System.out.println(doubleSum);
//list–T 泛型求和
long num = list.stream().mapToLong(User::getNum).sum();
Double cnt= list.stream().mapToDouble(ScreenSales::getCnt).sum();
参考链接:
https://developer.ibm.com/zh/articles/j-lo-java8streamapi/
博客围绕Java8 Stream API展开,介绍了对集合对象的多种操作。以学生类为例,展示了提取某列、列去重、过滤List对象、对List集合排序等操作,还提及利用stream lambda查询map中最大value及对应key,以及list泛型求和等内容。
680

被折叠的 条评论
为什么被折叠?



