Java8--Lambda表达式对List的操作
实体类:
public class Apple { private Integer id; private String name; private BigDecimal money; private Integer num; public Apple(Integer id, String name, BigDecimal money, Integer num) { this.id = id; this.name = name; this.money = money; this.num = num; } public Integer getId() { return id; } public String getName() { return name; } public BigDecimal getMoney() { return money; } public Integer getNum() { return num; } @Override public String toString() { return "Apple{" + "id=" + id + ", name='" + name + '\'' + ", money=" + money + ", num=" + num + '}'; } }
public class User { private String name; //age private int age; //分数 private double fraction; public String getName() { return name; } public int getAge() { return age; } public double getFraction() { return fraction; } public User(String name, int age, double fraction) { this.name = name; this.age = age; this.fraction = fraction; } }
填充List:
List<Apple> appleList = new ArrayList<>();//存放apple对象集合 Apple apple1 = new Apple(1, "苹果1", new BigDecimal("3.25"), 10); Apple apple12 = new Apple(1, "苹果2", new BigDecimal("1.35"), 20); Apple apple2 = new Apple(2, "香蕉", new BigDecimal("2.89"), 30); Apple apple3 = new Apple(3, "荔枝", new BigDecimal("9.99"), 40); appleList.add(apple1); appleList.add(apple12); appleList.add(apple2); appleList.add(apple3);
List<User> userList = new ArrayList<>();
userList.add(new User("a1", 22, 2.2));
userList.add(new User("a2", 22, 2.5));
userList.add(new User("a3", 40, 2.7));
userList.add(new User("a4", 45, 2.8));
List<String> list1 = new ArrayList<>();
list1.add("111");
list1.add("222");
list1.add("333");
List<String> list2 = new ArrayList<>();
list2.add("111");
list2.add("333");
list2.add("444");
List -> Map:
//List 以ID分组 Map<Integer,List<Apple>> Map<Integer, List<Apple>> groupBy = appleList.stream().collect(Collectors.groupingBy(Apple::getId)); System.out.println("groupBy:" + groupBy); /** * List -> Map * 需要注意的是: * toMap 如果集合对象有重复的key,会报错Duplicate key .... * apple1,apple12的id都为1。 * 可以用 (k1,k2)->k1 来设置,如果有重复的key,则保留key1,舍弃key2 */ Map<Integer, Apple> map = appleList.stream().collect(Collectors.toMap(Apple::getId, a -> a, (k1, k2) -> k1)); for (Map.Entry<Integer, Apple> entry : map.entrySet()) { System.out.println("key:" + entry.getKey() + "\nvalue:" + entry.getValue()); }
过滤、去重、计算、定位取、取某个元素重新组成List:
//计算 总金额 BigDecimal totalMoney = appleList.stream().map(Apple::getMoney).reduce(BigDecimal.ZERO, BigDecimal::add); System.out.println("totalMoney:" + totalMoney); //totalMoney:17.48 //去重 List<Apple> appleDistinctList = appleList.stream() .collect(Collectors.collectingAndThen (Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(t -> t.getId()))), ArrayList::new ) ); System.out.println("appleDistinctList:" + appleDistinctList); //取出属性中的某个值 重新组成list List<String> appleNameList = appleList.stream() .map(Apple::getName).collect(Collectors.toList()); System.out.println("appleNameList:"+appleNameList); //过滤名字为苹果的元素 List<Apple> appleList1 = appleList.stream().filter(x -> x.getName().equals("苹果1")) .collect(Collectors.toList()); System.out.println("appleList1:"+appleList1); //取前两位元素 List<Apple> limitList = appleList.stream().limit(2).collect(Collectors.toList()); System.out.println("limitList:"+limitList); //取后两位元素 List<Apple> skipList = appleList.stream().skip(2).collect(Collectors.toList()); System.out.println("skipList:"+skipList); //取第三位元素 List<Apple> collectList = appleList.stream().limit(3).skip(2).collect(Collectors.toList()); System.out.println("collectList:"+collectList);
排序(升序、降序、多条件排序):
//返回 对象集合以UsergetAge升序排序:年龄 --默认升序 userList.stream().sorted(Comparator.comparing(User::getAge)); //返回 对象集合以UsergetAge降序排序 ===这里写在前面 和写在后面要看清楚,弄明白 userList.stream().sorted(Comparator.comparing(User::getAge).reversed()); //排序结果后再排序, userList.stream().sorted(Comparator.comparing(User::getAge, Comparator.reverseOrder()));//是直接进行排序 //返回 对象集合以UsergetAge升序排序:**年龄并返回前n个元素** --默认升序 ==下面同样写法 userList.stream().sorted(Comparator.comparing(User::getAge)).limit(2); //返回 对象集合以UsergetAge升序排序:**年龄并去除前 n 个元素** --默认升序 ==下面同样写法 userList.stream().sorted(Comparator.comparing(User::getAge)).skip(2); //返回 对象集合以UsergetAge升序 getFraction升序 userList.stream().sorted(Comparator.comparing(User::getAge).thenComparing(User::getFraction)); //先以getAge升序,升序结果进行getAge降序,再进行getFraction升序 userList.stream().sorted(Comparator.comparing(User::getAge).reversed().thenComparing(User::getFraction)); //先以getAge降序,再进行getFraction升序 userList.stream().sorted(Comparator.comparing(User::getAge, Comparator.reverseOrder()).thenComparing(User::getFraction)); //先以getAge升序,升序结果进行getAge降序,再进行getFraction降序 userList.stream().sorted(Comparator.comparing(User::getAge).reversed().thenComparing(User::getFraction, Comparator.reverseOrder())); //先以getAge降序,再进行getFraction降序 userList.stream().sorted(Comparator.comparing(User::getAge, Comparator.reverseOrder()).thenComparing(User::getFraction, Comparator.reverseOrder())); //先以getAge升序,升序结果进行getAge降序,再进行getFraction升序,结果进行getAge降序getFraction降序 userList.stream().sorted(Comparator.comparing(User::getAge).reversed().thenComparing(User::getFraction).reversed()); //先以getAge升序,再进行getFraction降序 userList.stream().sorted(Comparator.comparing(User::getAge).thenComparing(User::getFraction, Comparator.reverseOrder()));
取交集、差集、并集:
// 交集 List<String> intersection = list1.stream().filter(item -> list2.contains(item)).collect(Collectors.toList()); System.out.println("---得到交集 intersection---");//333 111 intersection.parallelStream().forEach(System.out :: println); // 差集 (list1 - list2) List<String> reduce1 = list1.stream().filter(item -> !list2.contains(item)).collect(Collectors.toList()); System.out.println("---得到差集 reduce1 (list1 - list2)---");//222 reduce1.parallelStream().forEach(System.out :: println); // 差集 (list2 - list1) List<String> reduce2 = list2.stream().filter(item -> !list1.contains(item)).collect(Collectors.toList()); System.out.println("---得到差集 reduce2 (list2 - list1)---");//444 reduce2.parallelStream().forEach(System.out :: println); // 并集 List<String> listAll = list1.parallelStream().collect(Collectors.toList()); List<String> listAll2 = list2.parallelStream().collect(Collectors.toList()); listAll.addAll(listAll2); System.out.println("---得到并集 listAll---");//111 222 333 111 333 444 listAll.parallelStream().forEach(System.out :: println); // 去重并集 List<String> listAllDistinct = listAll.stream().distinct().collect(Collectors.toList()); System.out.println("---得到去重并集 listAllDistinct---");//111 222 333 444 listAllDistinct.parallelStream().forEach(System.out :: println); // 去重并集 Set<String> set = listAll.stream().collect(Collectors.toSet()); System.out.println("---得到去重并集 set---");//111 222 333 444 set.parallelStream().forEach(System.out :: println); System.out.println("---原来的List1---");//111 222 333 list1.parallelStream().forEach(System.out :: println); System.out.println("---原来的List2---");//111 333 444 list2.parallelStream().forEach(System.out :: println);
鸣谢:
https://www.cnblogs.com/john8169/p/9780524.html
https://www.cnblogs.com/miaoying/p/13042091.html
https://blog.csdn.net/gzt19881123/article/details/78327465/