• 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("行邮箱格式有误,请调后再次上传");
    
  • 相关阅读:
    JS学习之旅2
    JS学习之旅1
    Stack 栈
    Linked List 链表
    Array 数组
    时间/空间复杂度
    What/Why/How
    Https 握手过程
    JS跨域解决方案
    JS 的内存管理-GC
  • 原文地址:https://www.cnblogs.com/dulinan/p/12033003.html
Copyright © 2020-2023  润新知