java8关于集合的一些处理快捷方式

java8关于集合的一些快捷处理方式

1:对集合对象的某个属性进行字符串拼接


//对typeName属性进行斜杠拼接,当然斜杠也可以替换为逗号等各种符号

String typeName = list.stream().map(r ->r.getTypeName()).collect(Collectors.joining("/"));

2 :根据对象属性进行过滤


 //单个条件筛选:筛选出type等于1的信息
 
 List <Model> filterList=list.stream().filter(model -> "1".equals(model.getType())).collect(Collectors.toList());
    
//多个条件筛选:筛选出 type等于1且num等于3的的信息

list.stream().filter(model->"1".equals(model.getType())&&"2".equals(model.getPid())).collect(Collectors.toList());

3:对集合进行排序:默认升序;若是想降序加上reversed;多个属性排序用thenComparing


couponList = infoList.stream()
            .sorted(Comparator.comparing(BaseCouponInfo::getUsableValue).thenComparing(BaseCouponInfo::getCouponType).reversed().thenComparing(BaseCouponInfo::getParValue).reversed())
            .collect(Collectors.toList());


//单个字段升序处理
        matchItemList = matchItemList.stream().sorted(Comparator.comparing(BaseSalaryDetailItem::getXmCode,Comparator.nullsLast(String::compareTo)))
                    .collect(Collectors.toList());//升序
        
 //默认升序 :objType  
 Collections.sort(incomeList, Comparator.comparing(YfjhRzzsProfitDataVO::getObjType));
 //降序处理 :objType
 Collections.sort(incomeList, Comparator.comparing(YfjhRzzsProfitDataVO::getObjType).reversed());
            

4:判断对象集合中的某个属性是否包含某个值。


   //判断集合的id(Long类型)属性是否包含Long类型的2
   
   boolean a = list.stream().anyMatch(m -> m.getCenterId().equals(2L));
   boolean b = list.stream().anyMatch(m -> m.getCenterId().toString().equals("2"));
   boolean c = list.stream().anyMatch(m -> m.getCenterId()==2L);
   boolean d = list.stream().anyMatch(m -> m.getCenterId()==new Long(2));

	//返回结果
	
	a:true
	b:true
	c:true
	d:false
	
	两个Long类型比较建议equals比较。

5:将集合中对象的某个属性提取出来组成新的集合


List<Long> IdList=list.stream().map(Model::getId).collect(Collectors.toList());

6:判断非对象类型集合中是否存在重复元素;转化set集合

//判断list是否存在重复元素

List<Long> list=Arrays.asList(1L,1L,2L,3L,4L,5L,6L);
Set<Long> set = new HashSet<>(list);
if(set.size()== list.size()){
System.out.println("集合不存在重复元素");
}else{
System.out.println("集合存在重复元素");
}

7:针对对象类型集合的某个bigdecimal属性求和(保证部分值为null的情况不报异常)

BigDecimal money=  list.stream().map(e->e.Money()==null?new BigDecimal(0):e.Money()).reduce(BigDecimal.ZERO, BigDecimal::add);

8:取集合的前几位元素

	// 获取集合的前3个元素
  list = list.stream().limit(3).collect(Collectors.toList());


9:特定符号分割字符串组成集合

String ks="";
System.out.println(Arrays.asList(ks.split(",")));

10:判断两个集合是否有交集,获取交集

cn.hutool.core.collection.CollectionUtil工具类

1:判断是否有交集
CollectionUtil.containsAny(list1,list2)
若有则返回true;没有返回false
注意;list1,list2只要有一个空集合则回返回异常

2:获取交集元素
 List<Integer> result = CollUtil.intersection(list1, list2);


11:集合不为空的情况,但数据是All elements is null,如何删除空数据对象

shareList 集合
shareList.removeAll(Collections.singleton(null));

       if (CollectionUtils.isNotEmpty(shareList) && StringUtils.isNull(shareList.get(0))){
            shareList.removeAll(Collections.singleton(null));
        }

12:获取一个对象集符合另一个对象集合条件的信息

//集合1
List<HrOrgSalary> salaryList= hrOrgSalaryMapper.selectHrOrgSalaryNotSetShareList(salaryQuery);

//集合2
List<SysDept> centerDeptList=  sysDeptMapper.selectAllCenterDeptList();
centerDeptList=centerDeptList.stream().filter(x->!x.getDeptAttr().equals(DeptConstants.DEPT_TYPE_Yfjh)).collect(Collectors.toList());
List<String> matchCenterCode = centerDeptList.stream().map(SysDept::getCenterCode).collect(Collectors.toList());

// 获取集合1的CenterCode信息存在集合2的信息
List<HrOrgSalary> matchSuccessSalaryList = salaryList.stream().filter(a -> matchCenterCode.contains(a.getHrCenterCode())).collect(Collectors.toList());

//获取集合1的CenterCode信息不存在集合2的信息
List<HrOrgSalary> matchFailSalaryList = salaryList.stream().filter(a -> !matchCenterCode.contains(a.getHrCenterCode())).collect(Collectors.toList());

13:根据对象集合的某个属性进行分组

// 根据hrCenterCode 信息进行分组
Map<String,List<HrOrgSalary>> groupList=matchSuccessSalaryList.stream().collect(Collectors.groupingBy(HrOrgSalary::getHrCenterCode));

for (String centerCode:groupList.keySet()) {
    List<HrOrgSalary>  groupSalaryList=groupList.get(centerCode);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

博客胡

你的鼓励是我最大的动力

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

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

打赏作者

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

抵扣说明:

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

余额充值