日常开发总结:
- list 取某字段生成数组
String[] mProductIds = mProductList.stream().map(MProduct::getId).toArray(String[]::new);//遍历获取ids
- 数组 拼成字符串
String ids = Arrays.stream(mProductIds).map(String::toString).collect(Collectors.joining(","));
- 多条相同商品的情况,转换为map<pid, List<record>>形式
Map<Integer, List<MWarehouseProSaveInfo>> productMap = reqVo.getRecords().stream().collect(Collectors.groupingBy
(MWarehouseProSaveInfo::getPid));
- list<T> 以某个字段为类型生成一个list。
List<Integer> ids = mProductCategoryList.stream().map(MProductCategory::getId).collect(Collectors.toList());
- list -> String[]
String[] productFields = fieldList.stream().toArray(String[]::new);
- String[] -> List<>
去除list中重复数据
ArrayList<Employee> distinctedList = list.stream()
.collect(
Collectors.collectingAndThen(
Collectors.toCollection(() -> new TreeSet<>(Comparator.comparingLong(Employee::getId))),
ArrayList::new));
- Array -> list
result = Arrays.stream(lines).collect(Collectors.toList());
- list -> Array
list.stream().map(String::toString).toArray(String[]::new)
- 去重list 只保留第一个
ArrayList<Employee> distinctedList = list.stream()
.collect(
Collectors.collectingAndThen(
Collectors.toCollection(() -> new TreeSet<>(Comparator.comparingLong(Employee::getId))),
ArrayList::new)
);
- 分组 根据某字段
Map<String, List<MProductTag>> collect = allTagList.stream().collect(Collectors.groupingBy(MProductTag::getId));
- map:获取list某个字段的集合
List<Integer> categoryTypeList = categoryList.stream().map(e -> e.getCategoryType()).collect(Collectors.toList());
- 获取对象list中某字段 的 重复值
arrays.stream()
.collect(Collectors.groupBy(a->a.getField(),Collectors.counting())
.entrySet.stream()
.filter(entry->entry.getValue()>1)
.map(entry->entry.getKey())
.collect(Collectors.toList());
- 筛选
list.stream().filter(x -> "1".equals(x.getStatus)).collect(Collectors.toList());
- list >>> map
Map<String, String> ticketMap = ticketProductList.stream().collect(Collectors.toMap(MProduct::getCompareId, MProduct::getName));
- 扁平化 List<List<String>> list_1 --> List<String> list_2
list_2 = list_1.stream()
.flatMap(Collection::stream)
.collect(Collectors.toList());
list 根据某字段排序 List<CouponVO> newList = couponViews.stream().sorted(Comparator.comparing(CouponVO::getDenomination)) .collect(Collectors.toList());
//按User的年龄正序排序(默认ASC排序)
List<User> collect users.stream().sorted(Comparator.comparing(User::getAge)).collect(Collectors.toList());
System.out.println(collect);
打印结果:
[{name = '陈二',age =23},{name = '陈一',age =32},{name = '陈三',age =33}]
//按User的年龄倒序排序(DESC排序)
List<User> collect users.stream().sorted(Comparator.comparing(User::getAge).reversed()).collect(Collectors.toList());
动态生成list
public List<CouponCateRela> generateCouponCateList(CouponInfo couponInfo, List<String> cateIds) { return cateIds.stream().map((cateId) -> { CouponCateRela cateRela = new CouponCateRela(); cateRela.setPlatformFlag(couponInfo.getPlatformFlag()); cateRela.setCouponId(couponInfo.getCouponId()); cateRela.setCateId(cateId); return cateRela; }).collect(Collectors.toList()); }
-------------------------------------------------
合并两个list
方法一:使用addAll()方法将第二个List添加到第一个List中。
List<String> list1 = new ArrayList<>();
list1.add("A");
list1.add("B");
List<String> list2 = new ArrayList<>();
list2.add("C");
list2.add("D");
list1.addAll(list2);
System.out.println(list1); // [A, B, C, D]
方法二:使用Collections工具类中的addAll()方法将第二个List添加到第一个List中。
List<String> list1 = new ArrayList<>();
list1.add("A");
list1.add("B");
List<String> list2 = new ArrayList<>();
list2.add("C");
list2.add("D");
Collections.addAll(list1, list2.toArray(new String[list2.size()]));
System.out.println(list1); // [A, B, C, D]
方法三:使用Stream的concat()方法将两个List合并成一个。
List<String> list1 = new ArrayList<>();
list1.add("A");
list1.add("B");
List<String> list2 = new ArrayList<>();
list2.add("C");
list2.add("D");
List<String> mergedList = Stream.concat(list1.stream(), list2.stream())
.collect(Collectors.toList());
System.out.println(mergedList); // [A, B, C, D]
----------------------------------------
获取list中某个元素的最大 最小值
persons.stream()
.sorted((p1, p2) -> p2.getAge() - p1.getAge())
.limit(1).forEach((person) -> {
System.out.println("sorted+limit:" + JSON.toJSONString(person));
// sorted+limit:{"age":56,"name":"老周"}
});