使用stream流操作集合记录

记录使用stream流操作集合的写法

从集合中找出 EnableStatus 属性值等于 X 的值

List<Entity> collect = entities.stream().filter(item -> item.getEnableStatus().equals("X")).collect(Collectors.toList());

根据集合内 color 属性值进行分组

Map<String,List<Vo>> map = vos.stream().collect(Collectors.groupingBy(Vo::getColor));

根据集合内某属性进行去重

	// 创建方法: 
    private static <T> Predicate<T> distinctByCode(Function<? super T, Object> keyExtractor) {
    
    
        Map<Object, Boolean> seen = new ConcurrentHashMap<>();
        return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
    }
    //  使用:
     result = list.stream().filter(distinctByCode(Vo::getCode)).collect(Collectors.toList());

集合中的 ImgUrl 属性值转为字符串

String out = vos.stream().map(vo::getImgUrl).collect(Collectors.joining());

排序

List<Entity> sortList = entityList.stream().sorted(Comparator.comparing(e -> Integer.valueOf(e.getOrderNum()))).collect(Collectors.toList());

指定字段转map

Map<String, String> typeValMap = typeList.stream().collect(Collectors.toMap(DataEntity::getDictValue, DataEntity::getDictCode));

获取某字段与对应实体的集合

Map<String, DataEntity> typeValMap = typeList.stream().collect(Collectors.toMap(DataEntity::getDictValue, Function.identity(),(key1, key2) -> key2));
此处是可能存在问题的,存在空指针以及map内key重复问题
重复key解决方案 :
  1. 重复时用后面的value覆盖前面的value
Map<String, String> map = typeList.stream().collect(Collectors.toMap(DataEntity::getDictValue, DataEntity::getDictCode,(key1 , key2)-> key2 ));
  1. 重复时将前面的value和后面的value拼接起来展示
Map<String, String> map = typeList.stream().collect(Collectors.toMap(DataEntity::getDictValue, DataEntity::getDictCode,(key1 , key2) ->  key1 + "," + key2 ));
  1. 重复时将重复key的数据组成集合
Map<String, List<String>> map = list.stream().collect(Collectors.toMap(DataEntity::getDictValue,
				item ->  {
    
    
	    		 	List<String> listCode = new ArrayList<>();
	    		 		listCode .add(item .getDictCode());
	    		 		return listCode ;
    		 	},
   		     (List<String> value1, List<String> value2) -> {
    
    
   		     		value1.addAll(value2);
   		     		return value1;
   		     	}
	    		 ));
空指针解决方案 :
  1. 与上面的方法3是有点类似的,在转换流中加上判空,即便value为空,依旧输出
Map<String, List<String>> map = list.stream().collect(Collectors.toMap(DataEntity::getDictValue,
	    		item  ->  {
    
    
	    		 	List<String> listCode  = new ArrayList<>();
	    		 		listCode .add(p.getDictCode());
	    		 		return listCode ;
	    		 	},
    		     	(List<String> value1, List<String> value2) -> {
    
    
    		     		value1.addAll(value2);
    		     		return value1;
    		     	}
	    		 ))

取出某个值并处理为集合

List<String> roleCodes = list().stream().map(DetailVo ::getRoleCode).collect(Collectors.toList());

取出某个值并处理为字符串

String roleCodes = list().stream().map(DetailVo ::getRoleCode).collect(Collectors.joining());

其他的方便操作

取一个数的相反数

Integer integer = 33;
int i = (~(integer) + 1);
输出i即为 -33

集合转为固定字符分割的字符串

 String join = String.join(";", list); 此处‘;’是可替换的,换为自己需要的字符

利用stream进行不同类型的求和

BigDecimalBigDecimal b=list.stream().map(Plan::getAmount).reduce(BigDecimal.ZERO,BigDecimal::add);`
`intdoublelongdouble max = list.stream().mapToDouble(User::getHeight).sum();`
`IntegerIntSummaryStatistics collect = list.stream().collect(Collectors.summarizingInt(value -> value));
System.out.println("集合元素个数:" + collect.getCount());
System.out.println("集合元素累加:" + collect.getSum());
System.out.println("集合中最小值:" + collect.getMax());
System.out.println("集合中最大值:" + collect.getMin());
System.out.println("集合中平均值:" + collect.getAverage());

猜你喜欢

转载自blog.csdn.net/weixin_44974020/article/details/111384433