1. Convert list to map

     Map<Long, ItemTO> itemMap =
        items.stream().collect(Collectors.toMap(ItemTO::getEventId, Function.identity(), (v1, v2)->v1));
  2. A property value of an object in a List is taken out as a list
 // 去重
List<Long> ids = items.stream().map(ItemTO::getId).distinct().collect(Collectors.toList());

// 不去重
List<Long> ids = items.stream().map(ItemTO::getId).collect(Collectors.toList());
  1. Convert list to Map<Long, List>
 Map<Long, List<ItemTO>> itemListMap = itemList.stream().collect(Collectors.groupingBy(item -> item.getId()));

Map<Long, ItemTO> itemMap =
        items.stream().collect(Collectors.toMap(ItemTO::getEventId, Function.identity(), (v1, v2)->v1));
  1. Convert string in list to long
 List<Long> idList = groupList.stream().map(group->Long.parseLong(group.getGroupLeaderId())).collect(Collectors.toList());
  1. filter operation
 groupIdList.stream().filter(x -> x!=null).collect(Collectors.toList());


List<Long> allPlatformIids = allStoreItemIndexResultTOS.stream().filter(t -> StoreItemTypeEnum.PLATFORM.getType()
      .equals(t.getType())).map(StoreItemIndexResultTO::getIid).collect(Collectors.toList());

//过滤采购数量为0的sku
List<ObmSkuInfoTO> skuCollectList = skuInfoTOList.stream().
        filter(e -> e.getNum() > 0).collect(Collectors.toList());


/**
 * 在订单列表中过滤指定的oid
 * @param effectiveLists
 * @param oid
 * @return
 */
private List<ItemTO> filterOrder(List<ItemTO> itmeList, long oid) {
    return itmeList.stream().filter(p -> {
        if (p.getOid().equals(oid)) {
            return false;
        }
        return true;
    }).collect(Collectors.toList());
}
  1. Use the two attribute values of the object in the list as the key and value of the map respectively
 itemList.stream().filter(t -> t.getId() != null)
        .collect(Collectors.toMap(ItemTO::getId, ItemTO::getName, (k1, k2)->k2));
  1. operate on the list
 // 交集
List<String> intersection = list1.stream().filter(item -> list2.contains(item)).collect(toList());

// 差集 (list1 - list2)
List<String> reduce1 = list1.stream().filter(item -> !list2.contains(item)).collect(toList());

// 差集 (list2 - list1)
List<String> reduce2 = list2.stream().filter(item -> !list1.contains(item)).collect(toList());

// 并集
List<String> listAll = list1.parallelStream().collect(toList());
List<String> listAll2 = list2.parallelStream().collect(toList());
listAll.addAll(listAll2);

// 去重并集
List<String> listAllDistinct = listAll.stream().distinct().collect(toList());
System.out.println("---得到去重并集 listAllDistinct---“);

// 循环输出
listAllDistinct.parallelStream().forEachOrdered(System.out :: println);
  1. map operation, remove
 // 通过value移除
map.values().removeIf(value -> !value.contains("1"));

// 通过key移除
map.keySet().removeIf(key -> key != 1);

// key或者value移除
map.entrySet().removeIf(entry -> entry.getKey() != 1);
  1. Sort operation
 // 升序
itemList = itemList.stream().sorted(Comparator.comparingInt(ItemTO::getPtPrice)).collect(Collectors.toList());
// 降叙
itemList = itemList.stream().sorted(Comparator.comparingInt(ItemTO::getPtPrice).reversed()).collect(Collectors.toList());
  1. groupBy operation
 //a
Map<Long,List<Long>> exhibitionPitemMap = list.stream().collect(Collectors.groupingBy(TestDTO1::getLevle1CategoryId, Collectors.mapping(TestDTO1::getPitemId, Collectors.toList())));
//b
Map<Long, List<TestDTO2>> categoryPitemMap = list.stream().collect(Collectors.groupingBy(TestDTO2::getLevle1CategoryId));
  1. map to list usage
 Map<String, String> map = new HashMap<>(); 
// Convert all Map keys to a List
List<String> result = new ArrayList(map.keySet()); 

// Convert all Map values to a List
List<String> result2 = new ArrayList(map.values()); 

// Java 8, Convert all Map keys to a List
List<String> result3 = map.keySet().stream() .collect(Collectors.toList());

// Java 8, Convert all Map values to a List
List<String> result4 = map.values().stream() .collect(Collectors.toList());

// Java 8, seem a bit long, but you can enjoy the Stream features like filter and etc.
List<String> result5 = map.values().stream() .filter(x -> !"apple".equalsIgnoreCase(x)) .collect(Collectors.toList());
  1. Modify the value of an element in a list
 list.stream().filter(a -> 筛选条件).forEach(b -> b.setOrg("1"));
  1. Use the two attributes of the elements in the list as the key and value of the map respectively
 Map<Long, String> idNameMap = userList.stream().collect(Collectors.toMap(UserDO::getId, UserDO::getName, (v1, v2)->v1));

hizengzeng
177 声望10 粉丝

hizengzeng