java8 方便好用的方法

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/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

strggle_bin

一毛不嫌少,十元不嫌多

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值