1/** 2 * 3 * Description: JDK1.8的Stream操作工具类 4 * @author linan.du 5 * @date 2019年7月18日 6 * @version 1.0 7 */ 8public class StreamUtil { 9 10 /** 11 * 12 * Description: stream去重时,调用它可免除 泛型重写equals和hashcode方法 13 * 14 * 调用示例: 15 * reportPoolList是查询出来的List<ReportPool>集合,通过过滤,对postCode属性(岗位代码)进行去重,返回 16 * List<ReportPool> distinctElements = reportPoolList.stream() 17 * .filter(StreamUtil.distinctByKey(p -> p.getPostcode())) 18 * .collect(Collectors.toList()); 19 * 20 * @param keyExtractor 21 * @return 22 * @author linan.du 23 * @date 2019年7月18日 24 * 25 */ 26 public static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) { 27 Set<Object> seen = ConcurrentHashMap.newKeySet(); 28 return t -> seen.add(keyExtractor.apply(t)); 29 } 30}
-
补充
// 您上传文件中第1、2、5行邮箱格式有误,请调后再次上传StringBuffer sb = new StringBuffer(); sb.append("您上传文件中第"); errorList.stream().forEach(e -> sb.append(e.getSeq() + "、")); sb.deleteCharAt(sb.length() - 1).append("行邮箱格式有误,请调后再次上传");