java8 stream 集合去重

/**
 * 
 * Description: JDK1.8的Stream操作工具类
 * @author linan.du
 * @date 2019年7月18日  
 * @version 1.0
 */
public class StreamUtil {

    /**
     * 
     * Description: stream去重时,调用它可免除 泛型重写equals和hashcode方法
     * 
     * 调用示例:
     * reportPoolList是查询出来的List<ReportPool>集合,通过过滤,对postCode属性(岗位代码)进行去重,返回
     * List<ReportPool> distinctElements = reportPoolList.stream()
     *                                     .filter(StreamUtil.distinctByKey(p -> p.getPostcode()))
     *                                     .collect(Collectors.toList());
     * 
     * @param keyExtractor
     * @return
     * @author linan.du
     * @date 2019年7月18日 
     *
     */
    public static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) {
        Set<Object> seen = ConcurrentHashMap.newKeySet();
        return t -> seen.add(keyExtractor.apply(t));
    }
}
  • 补充
    // 您上传文件中第1、2、5行邮箱格式有误,请调后再次上传

    StringBuffer sb = new StringBuffer(); sb.append("您上传文件中第"); errorList.stream().forEach(e -> sb.append(e.getSeq() + "、")); sb.deleteCharAt(sb.length() - 1).append("行邮箱格式有误,请调后再次上传");

评论

h
hw_7864

⭐️ Admin快速开发: https://show.cool-admin.com/

赞 0
h
hw_7864

⭐️ 官方文档: https://cool-js.com

赞 0
h
hw_7864

⭐️ chatgpt在线体验: https://cool-js.com/ai/chat-vip/index.html?r=helloworld

赞 0