JDK8中stream经常用到的方法集合

将Restriction对象中的value属性取出并逗号拼接

String str = restrictionList.stream().map(Restriction::getValue).collect(Collectors.joining(","));


将Restriction对象中的id属性逗号拼接后转换为Strnig类型

String str = restrictionList.stream().map(Restriction::getId).collect(Collectors.toList()).stream().map(w->w.toString()).collect(Collectors.joining(","));


去重

// 根据name去重

List<Person> unique = persons.stream().collect(

            collectingAndThen(

                    toCollection(() -> new TreeSet<>(comparing(Person::getName))), ArrayList::new)

);

// 根据name,sex两个属性去重

List<Person> unique = persons.stream().collect(

            collectingAndThen(

                    toCollection(() -> new TreeSet<>(Comparator.comparing(o -> o.getName() + ";" + o.getSex()))), ArrayList::new)

);


根据FullName和CancelTime筛选

List<BlacklistUser> buList = SystemInitVariable.blacklistUserList.stream().filter(bu ->

    bu.getUsername().equals(us.getFullName()) && bu.getCancelTime().after(currentTime)

).collect(Collectors.toList());


将字符串集合中的数据批量下划线分隔

Set strs =new HashSet<>();

strs.add("a_a");

strs.add("b_b");

strs.add("c_c");

Set strsss = strs.stream().map(str -> str.substring(str.indexOf("_")+1)).collect(Collectors.toSet());

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容