1、筛选:filter
List<UserDTO> userDTOS = new ArrayList<>();
List<String> userTypeCodeList = userDTOS.stream().filter(user -> "YR".equals(user.getType()) || "YL".equals(user.getType())).collect(Collectors.toList());
2、根据指定的字段去重,留下相同code记录中updateDate最大的那个
List<DataResDTO> list = mapper.getShipmentData(companyCode);
List<DataResDTO> resultList = list.stream() .collect(Collectors.groupingBy(DataResDTO::getCode, Collectors.collectingAndThen( Collectors.maxBy(Comparator.comparing(DataResDTO::getUpdateDate)), Optional::get))) .values() .stream() .collect(Collectors.toList());
3、筛选同一个outletNo对应的所有usage,用逗号连接成字符串items,然后用构造函数写入outletNo和items到新的实体类OutletWhite中
@Data
@AllArgsConstructor
public class OutletWhite implements Serializable {
private static final long serialVersionUID = 1L;
private String outletNo;
private String items;
}
List<OutletWhiteList> ttList= service.getOutletWhiteList(companyCode);
List<OutletWhite> outletWhiteList = ttList.stream() .collect(Collectors.groupingBy(OutletWhiteList::getOutletNo, Collectors
.mapping(OutletWhiteList::getUsage, Collectors.joining(",")))) .entrySet()
.stream()
.map(entry -> new OutletWhite(entry.getKey(), entry.getValue())) .collect(Collectors.toList());
4、字符串转数组
String[] groupNo = Arrays.stream(user.getGroupListString().split(","))
.filter(s -> !s.trim().isEmpty())
.sorted()
.collect(Collectors.joining(","));
5、字符串转列表
List<String> groupNoList = Arrays.stream(user.getGroupListString().split(","))
.map(String::trim)
.filter(s -> !s.isEmpty())
.sorted()
.collect(Collectors.toList());
6、去重,排序
list.stream().distinct().sorted(Comparator.comparing(User::getGroup).reversed() .thenComparing(User::getName)).collect(Collectors.toList());
7、组成map
List<Map> data = listResultData.getData();
DecimalFormat decimalFormat = new DecimalFormat("0.000");
Map<String, String> weightMap = data.stream() .filter(map -> map.containsKey("Code") && map.containsKey("Weight")) .collect(Collectors.toMap( map -> (String) map.get("Code"), map -> decimalFormat.format(map.get("Weight")), (k, y) -> k ));
8、属性名称相同的两个对象之间的复制
List<ResponseDTO> resDTOS = resBOS.stream().map( bo -> {
ResponseDTO resDTO = new ResponseDTO ();
BeanUtils.copyProperties(bo, resDTO );
return resDTO ;
}).collect(Collectors.toList());