一、list操作分组、过滤、求和、最值、排序、去重。

**
1、通过groupingBy可以分组指定字段

  1. // 分组
  2. Map<String, List<User>> groupBySex = userList.stream().collect(Collectors.groupingBy(User::getSex));
  3. // 遍历分组
  4. for (Map.Entry<String, List<User>> entryUser : groupBySex.entrySet()) {
  5. String key = entryUser.getKey();
  6. List<User> entryUserList = entryUser.getValue();
  7. }

多字段分组

  1. Function<WarehouseReceiptLineBatch, List<Object>> compositeKey = wlb ->
  2. Arrays.<Object>asList(wlb.getWarehouseReceiptLineId(), wlb.getWarehouseAreaId(), wlb.getWarehouseLocationId());
  3. Map<Object, List<WarehouseReceiptLineBatch>> map =
  4. warehouseReceiptLineBatchList.stream().collect(Collectors.groupingBy(compositeKey, Collectors.toList()));
  5. //遍历分组
  6. for (Map.Entry<Object, List<WarehouseReceiptLineBatch>> entryUser : map.entrySet()) {
  7. List<Object> key = (List<Object>) entryUser.getKey();
  8. List<WarehouseReceiptLineBatch> entryUserList = entryUser.getValue();
  9. Long warehouseReceiptLineId = (Long) key.get(0);
  10. Long warehouseAreaId = (Long) key.get(0);
  11. Long warehouseLocationId = (Long) key.get(0);
  12. }

2、过滤:通过filter方法可以过滤某些条件

  1. // 过滤
  2. // 排除掉工号为201901的用户
  3. List<User> userCommonList = userList.stream().filter(a -> !a.getJobNumber().equals("201901")).collect(Collectors.toList());

3、求和:分基本类型和大数类型求和,基本类型先mapToInt,然后调用sum方法,大数类型使用reduce调用BigDecimal::add方法

  1. // 求和
  2. // 基本类型
  3. int sumAge = userList.stream().mapToInt(User::getAge).sum();
  4. // BigDecimal求和
  5. BigDecimal totalQuantity = userList.stream().map(User::getFamilyMemberQuantity).reduce(BigDecimal.ZERO, BigDecimal::add);

上面的求和不能过滤bigDecimal对象为null的情况,可能会报空指针,这种情况,我们可以用filter方法过滤,或者重写求和方法

  1. package com.vvvtimes.util;
  2. import java.math.BigDecimal;
  3. public class BigDecimalUtils {
  4. public static BigDecimal ifNullSet0(BigDecimal in) {
  5. if (in != null) {
  6. return in;
  7. }
  8. return BigDecimal.ZERO;
  9. }
  10. public static BigDecimal sum(BigDecimal ...in){
  11. BigDecimal result = BigDecimal.ZERO;
  12. for (int i = 0; i < in.length; i++){
  13. result = result.add(ifNullSet0(in[i]));
  14. }
  15. return result;
  16. }
  17. }

使用重写的方法

  1. BigDecimal totalQuantity2 = userList.stream().map(User::getFamilyMemberQuantity).reduce(BigDecimal.ZERO, BigDecimalUtils::sum);

判断对象空

  1. stream.filter(x -> x!=null)
  1. stream.filter(Objects::nonNull)

判断字段空

  1. stream.filter(x -> x.getDateTime()!=null)

4、最值
求最小与最大,使用min max方法

  1. // 最小
  2. Date minEntryDate = userList.stream().map(User::getEntryDate).min(Date::compareTo).get();
  3. // 最大
  4. Date maxEntryDate = userList.stream().map(User::getEntryDate).max(Date::compareTo).get();

有时候我们需要知道最大最小对应的这个对象,我们可以通过如下方法获取

  1. Comparator<LeasingBusinessContract> comparator = Comparator.comparing(LeasingBusinessContract::getLeaseEndDate);
  2. LeasingBusinessContract maxObject = leasingBusinessContractList.stream().max(comparator).get();

5、List 转map

  1. /**
  2. * List -> Map
  3. * 需要注意的是:
  4. * toMap 如果集合对象有重复的key,会报错Duplicate key ....
  5. * user1,user2的id都为1。
  6. * 可以用 (k1,k2)->k1 来设置,如果有重复的key,则保留key1,舍弃key2
  7. */
  8. Map<Long, User> userMap = userList.stream().collect(Collectors.toMap(User::getId, a -> a,(k1,k2)->k1));

list转map的时候有时候会将date类型作为key,实际情况中使用string的多,我们可以将某个字段转成string

  1. Map<String, WorkCenterLoadVo> workCenterMap = list.stream().collect(Collectors.toMap(key->DateFormatUtils.format(key.getDate(), "yyyy-MM-dd"), a -> a,(k1,k2)->k1));

6、排序:可通过Sort对单字段多字段排序

  1. // 排序
  2. // 单字段排序,根据id排序
  3. userList.sort(Comparator.comparing(User::getId));
  4. // 多字段排序,根据id,年龄排序
  5. userList.sort(Comparator.comparing(User::getId).thenComparing(User::getAge));

7、去重:可通过distinct方法进行去重

  1. // 去重
  2. List<Long> idList = new ArrayList<Long>();
  3. idList.add(1L);
  4. idList.add(1L);
  5. idList.add(2L);
  6. List<Long> distinctIdList = idList.stream().distinct().collect(Collectors.toList());

针对属性去重

  1. List<AddOutboundNoticeDetailsBatchVo> entryDetailsBatchDistinctBatchIdList = entryDetailsBatchList.stream().filter(distinctByKey(b -> b.getMaterialBatchNumberId())).collect(Collectors.toList());
  2. //distinctByKey自己定义
  3. public static <T> Predicate<T> distinctByKey(Function<? super T, Object> keyExtractor) {
  4. Map<Object, Boolean> seen = new ConcurrentHashMap<>();
  5. return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
  6. }

8、获取list某个字段组装新list

  1. // 获取list对象的某个字段组装成新list
  2. List<Long> userIdList = userList.stream().map(a -> a.getId()).collect(Collectors.toList());

9、批量设置list列表字段为同一个值

  1. addList.stream().forEach(a -> a.setDelFlag("0"));

10、不同实体的list拷贝

  1. List<TimePeriodDate> timePeriodDateList1 = calendarModelVoList.stream().map(p->{TimePeriodDate e = new TimePeriodDate(); e.setStartDate(p.getBegin());e.setEndDate(p.getEnd()); return e;}).collect(Collectors.toList());

二、 map的多种循环方式

  1. Map<String, String> map = new HashMap<String, String>();
  2. // 第一种遍历方式
  3. System.out.println("第一种遍历方式:通过遍历 Map 的 keySet,遍历 Key 和 Value");
  4. for (String key : map.keySet()) {
  5. System.out.println("Key: " + key + ", Value: " + map.get(key));
  6. }
  7. // 第二种遍历方式(如果在遍历过程中,有删除某些Key-Value的需求,可以使用这种遍历方式)
  8. System.out.println("\n第二种遍历方式:通过Iterator 迭代器遍历 Key 和 Value");
  9. Iterator<Map.Entry<String, String>> iterator = map.entrySet().iterator();
  10. while(iterator.hasNext()) {
  11. Map.Entry<String, String> entry = iterator.next();
  12. System.out.println("Key: " + entry.getKey() + ", Value: " + entry.getValue());
  13. }
  14. // 第三种遍历方式(推荐,尤其是容量大时)
  15. System.out.println("\n第三种遍历方式:通过遍历 Map 的 entrySet,遍历 Key 和 Value");
  16. for (Map.Entry<String, String> entry : map.entrySet()) {
  17. System.out.println("Key: " + entry.getKey() + ", Value: " + entry.getValue());
  18. }
  19. // 第四种遍历方式
  20. System.out.println("\n第四种遍历方式:通过遍历 Map 的 values,只能遍历 Value,获取不到对应的 Key");
  21. for (String value : map.values()) {
  22. System.out.println("Value: " + value);
  23. }
  24. // 第五种遍历方式(JDK 1.8支持的 Lambda 表达式,强烈推荐!!!)
  25. System.out.println("\n第五种遍历方式:通过 Lambda 表达式,遍历 Key 和 Value");
  26. map.forEach((key, value) -> {
  27. System.out.println("Key: " + key + ", Value: " + value);
  28. });

三、Optional解决判断Null为空的问题

orElse、orElseGet的用法相当于value值为null时,给予一个默认值:
当user值为null时,orElse函数依然会执行createUsers()方法,而orElseGet函数并不会执行createUsers()

  1. User user = null;
  2. user = Optional.ofNullable(user).orElse(createUsers());
  3. user = Optional.ofNullable(user).orElseGet(() -> createUsers());
  4. public User createUsers(){
  5. User user = new User();
  6. user.setName("Test");
  7. return user;
  8. }

orElseThrow:当value值为null时,直接抛一个异常出去

  1. User user = null;
  2. Optional.orNullable(user).orElseThrow(() -> new Exception("用户不存在"))

判断一个user不为空时,执行业务代码

  1. Optional.ofNullable(user).ifPresent(u->{
  2. // TODO: do something
  3. });

取值某个对象属性的值

  1. // 以前写法
  2. public String getCity(User user) throws Exception{
  3. if(user!=null){
  4. if(user.getAddress()!=null){
  5. Address address = user.getAddress();
  6. if(address.getCity()!=null){
  7. return address.getCity();
  8. }
  9. }
  10. }
  11. throw new Excpetion("取值错误");
  12. }
  13. // java8新写法
  14. public String getCity(User user) throws Exception{
  15. return Optional.ofNullable(user)
  16. .map(u-> u.getAddress())
  17. .map(a->a.getCity())
  18. .orElseThrow(()->new Exception("取指错误"));
  19. }