天呐!代码还可以这么写。一波骚操作

最近到接触Java8新特性发现更新后的版本确定优化了很多,这里简单介绍下新特性中的某些骚操作;

利用Java8新特性,可以用简洁高效的代码来实现一些数据处理;

定义一个Persion对象:

@Data
@NoArgsConstructor  //无参构造
@AllArgsConstructor //有参构造
public class Persion {private Integer id;private String name;private String sex;private Integer age;}

创建一个集合,并添加一些测试数据:

List<Persion> persionList = newArrayList<>();
​
Persion persion0 = new Persion(1,"沉默木头人0","男",4);
Persion persion1 = new Persion(1,"沉默木头人1","男",5);
Persion persion2 = new Persion(2,"沉默木头人2","男",6);
Persion persion3 = new Persion(3,"沉默木头人3","男",7);
​
persionList.add(persion0);
persionList.add(persion1);
persionList.add(persion2);
persionList.add(persion3);

1、分组:

List里面的对象元素,以某个属性来分组,例如,以id分组,将相同的id放在一起。

//将list中的数据以id进行分组,相同的放在一起,放回Map
Map<Integer, List<Persion>> collect = persionList.stream().collect(Collectors.groupingBy(Persion::getId));
​
System.out.println("分组后得到的数据:"+collect);
分组后得到的数据:{1=[Persion(id=1, name=沉默木头人0, sex=, age=4), Persion(id=1, name=沉默木头人1, sex=, age=5)], 2=[Persion(id=2, name=沉默木头人2, sex=, age=6)], 3=[Persion(id=3, name=沉默木头人3, sex=, age=7)]}

2、List转Map:

将Persion中的id作为key,persion对象作为value,可以这么操作:

/**
 * 将id作为key,persion对象作为value转为map
 * 需要注意的是:toMap 如果集合对象有重复的key,会报错Duplicate key ....
 * Collectors.toMap(参数1, 参数2, 参数3)
 * 参数1:定key
 * 参数2:key-value  (a是变量表示集合中的元素)
 * 参数3:如果有重复的key,则保留key1,舍弃key2(可不填)
 */
Map<Integer, Persion> collect = persionList.stream().collect(Collectors.toMap(Persion::getId, a -> a, (k1, k2) -> k1));
System.out.println("转为Map:"+collect);
转为Map:{1=Persion(id=1, name=沉默木头人0, sex=, age=4), 2=Persion(id=2, name=沉默木头人2, sex=, age=6), 3=Persion(id=3, name=沉默木头人3, sex=, age=7)}

3、过滤Filter:

从List集合中过滤出来符合条件的元素。

/**
 * 从集合中过滤出符合条件的元素
 */
List<Persion> collect = persionList.stream().filter(a -> a.getName().equals("沉默木头人2")).collect(Collectors.toList());
​
System.out.println("过滤后得到的集合:"+collect);
过滤后得到的集合:[Persion(id=2, name=沉默木头人2, sex=, age=6)      

4、查找集合中最大最小值:

Collectors.maxBy 和 Collectors.minBy 来计算流中的最大或最小值。

//根据persion对象中age属性找出最大的一个元素
Optional<Persion> max = persionList.stream().collect(Collectors.maxBy(Comparator.comparing(Persion::getAge)));
max.ifPresent(System.out::println)

输出最大:

Persion(id=3, name=沉默木头人3, sex=, age=7)
//根据persion对象中age属性找出最小的一个元素
Optional<Persion> min = persionList.stream().collect(Collectors.minBy(Comparator.comparing(Persion::getAge)));
min.ifPresent(System.out::println);

输出最小:

Persion(id=1, name=沉默木头人0, sex=, age=4)

5、去重:

根据id去重。

//根据id去重
List<Persion> collect = persionList.stream().collect(
        collectingAndThen(
                toCollection(() -> new TreeSet<>(comparingLong(Persion::getId))), ArrayList::new
        )
);
System.out.println("去重之后的集合:"+collect);
去重之后的集合:[Persion(id=1, name=沉默木头人0, sex=, age=4), Persion(id=2, name=沉默木头人2, sex=, age=6), Persion(id=3, name=沉默木头人3, sex=, age=7)]

6、求和:

将集合中的数据按照某个属性求和,遗憾的是只能对BigDecimal和Double类型的属性进行求和,这里就简单演示一下BigDecimal类型的操作。

BigDecimal totalMoney = 集合名称.stream().map(类名::指定属性[getMoney]).reduce(BigDecimal.ZERO, BigDecimal::add);
System.err.println("totalMoney:"+totalMoney);  //totalMoney:17.48

常用 Collectors 类的静态工厂方法:
在这里插入图片描述


公众号:沉默木头人

优快云:沉默木头人(ID:qq_44322555)

喜欢感兴趣扫描下面二维码关注吧!

在这里插入图片描述

原创不易,不喜勿喷,如果能够帮助到你或对你有所启发欢迎下方留言。

喜欢就开始你无情的三连击:点赞、分享、关注。这将是我写作更多有趣有益有知的好文章的动力;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值