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("行邮箱格式有误,请调后再次上传");
原文地址:https://www.cnblogs.com/dulinan/p/12033003.html