Java8新特性Stream流之List、Map互转、去重、过滤

一、Streams(流)?

java.util.Stream 表示能应用在一组元素上一次执行的操作序列。Stream 操作分为中间操作或者最终操作两种,最终操作返回一特定类型的计算结果,而中间操作返回Stream本身,这样你就可以将多个操作依次串起来。Stream 的创建需要指定一个数据源,比如 java.util.Collection 的子类,List 或者 Set, Map 不支持。Stream 的操作可以串行执行或者并行执行。

二、具体使用

1.Filter(过滤)

过滤通过一个predicate接口来过滤并只保留符合条件的元素,该操作属于中间操作,所以我们可以在过滤后的结果来应用其他Stream操作(比如forEach)。forEach需要一个函数来对过滤后的元素依次执行。forEach是一个最终操作,所以我们不能在forEach之后来执行其他Stream操作。
代码如下(过滤开头字母为a的字符串):

stringList.stream().filter((s) -> s.startsWith("a")).forEach(System.out::println);//aaa2 aaa1

2.Sorted(排序)

排序是一个 中间操作,返回的是排序好后的 Stream。如果你不指定一个自定义的 Comparator 则会使用默认排序。
代码如下(示例):

stringList.stream().sorted().filter((s) -> s.startsWith("a")).forEach(System.out::println);

排序只创建了一个排列好后的Stream,而不会影响原有的数据源,排序之后原数据stringCollection是不会被修改的

3.Map(映射)

中间操作 map 会将元素根据指定的 Function 接口来依次将元素转成另外的对象,map返回的Stream类型是根据你map传递进去的函数的返回值决定的。

stringList.stream().map(String::toUpperCase)
.sorted((a, b) -> b.compareTo(a)).forEach(System.out::println);

4.实体类集合某一属性转成list

List<Long> assetIds = entityPlaybills.stream().map(EntityPlaybill::getAssetId)
.collect(Collectors.toList());

5.实体类集合转成map

1.key具体属性值,value实例对象

Map<String, LiveEventInfo> infoMap = 
liveEventInfos.stream().collect(Collectors.toMap(LiveEventInfo::getName, Function.identity(), 
(key1, key2) -> key2));

2.key具体属性值,value具体属性值
Map<Long,Integer> map = secondChannelInfos.stream().
collect(Collectors.toMap(SecondChannelInfo::getSecondChannelId, SecondChannelInfo::getFastStrip));

6.list集合属性去重

 List<Long> noDeleted = outChannelRepository.findByDeleted(0)
 .stream().map(OutChannelInfo::getOutChannelId).distinct().collect(Collectors.toList());

7.list集合属性过滤

1.
Map<Long, String> outMap = outChannelRepository.findByOutChannelIdIn(longs)
.stream().filter(s->s.getDeleted()==0)
.collect(Collectors.toMap(OutChannelInfo::getOutChannelId, OutChannelInfo::getName));

2.
List<String> stringList = outChannelResourceInfoList
.stream().filter(e -> Objects.nonNull(e.getSTaskId()))
.map(OutChannelResourceInfo::getSTaskId).collect(Collectors.toList());

8.reduce函数

 String emails = byIdIn.stream()
 .map(NoticeGroupInfo::getEmail).reduce((a,b) -> a+","+b).orElse("");

9.list实体类集合转换成一个新的实体类集合


List<EntityPlayBillLabelInfo> list = map.entrySet()
.stream().map(c -> new EntityPlayBillLabelInfo(c.getKey(), c.getValue()))
.collect(Collectors.toList());

10.list实体类集合根据某一属性去重

list.stream().filter(distinctByKey(User::getId)).collect(Collectors.toList());


private static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) {
    
    
        Map<Object, Boolean> seen = new ConcurrentHashMap<>();
        return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
    }



11.list实体类集合根据某一属性分组

// 根据SecondChannelId分组获得map
	Map<Long, List<OutChannelResourceInfo>> secondChannelIdMap = 
	rfs.stream()
	.collect(Collectors.groupingBy(OutChannelResourceInfo::getSecondChannelId));

//根据某一属性分组,对象中的另一属性收束为集合
        Map<String, List<String>> map = thsConfigDetails.stream()
        .collect(Collectors.groupingBy(PayThsConfigDetail::getThs_config_code, Collectors.mapping(PayThsConfigDetail::getThs_config_detail_value, Collectors.toList())));

12.list实体类集合多字段根据逗号拼接返回字符串

     String concatenatedString = objects.stream()
     .map(obj -> obj.getField1() + "-" + obj.getField2()) // 仅拼接 field1 和 field2
     .collect(Collectors.joining(","));

三、避坑问题

  1. stream流直接过滤出来的集合,变更集合对象属性时源数据会变

原因:在使用流(Stream)进行过滤操作时,如果直接修改过滤出来的集合对象的属性,会影响到源数据。这是因为流操作是基于源数据进行的,而流的操作是惰性执行的,只有在终止操作时才会触发实际的计算和处理。
当你使用流的过滤操作时,实际上是创建了一个中间操作的流,该流保留了对源数据的引用。因此,如果你修改了中间操作流中的对象的属性,实际上就是修改了源数据中对应对象的属性。

解决办法:
使用map操作来创建新的对象或集合,以确保不会改变源数据。

List<OutChannelPlaybill> firstListChannelPlaybills = firstChannelListPlaybills.stream()
.map(o -> BeanUtil.toBean(o, OutChannelPlaybill.class))
.collect(Collectors.toList());

おすすめ

転載: blog.csdn.net/hurtseverywhere/article/details/121371920