作者: tearell

  • JAVA流式编程案例

    JAVA流式编程案例

    分组操作

    对组元素个数进行计数

     public static void main(String[] args) {
            List<Person> people = Arrays.asList(
                    new Person("Alice", "female", 20),
                    new Person("Bob", "male", 25),
                    new Person("Charlie", "male", 30),
                    new Person("David", "male", 25),
                    new Person("Eve", "female", 20),
                    new Person("Frank", "male", 30)
            );
    
            // 按照年龄分组,计算每个年龄组的人数
            Map<Integer, Long> countByAge = people.stream()
                    .collect(Collectors.groupingBy(Person::getAge, Collectors.counting()));
    
            // 输出每个年龄组的人数
            countByAge.forEach((age, count) -> System.out.println(age + ": " + count));
    }

    对组元素求和

    public static void main(String[] args) {
            List<Person> people = Arrays.asList(
                    new Person("Alice", "female", 20),
                    new Person("Bob", "male", 25),
                    new Person("Charlie", "male", 30),
                    new Person("David", "male", 25),
                    new Person("Eve", "female", 20),
                    new Person("Frank", "male", 30)
            );
            // 按照性别分组,计算每个性别组的年龄总和
            Map<Integer, Long> countByAge = people.stream()
                    .collect(Collectors.groupingBy(Person::getMale, Collectors.summarizingInt(Person::getAge)));
            // 输出每个性别组的年龄总和
            countByAge.forEach((age, count) -> System.out.println(age + ": " + count));
    }

    将对象的集合,按照排序值,进行排序

    TreeMap<Long, List<YourEntity>> treeMap = this.list().stream()
    .sorted(Comparator.comparing(YourEntity::getSort))
    .collect(Collectors.groupingBy(YourEntity::getSort, TreeMap::new, Collectors.toList()));

    将对象的集合,按照自定义排序规则,进行排序

    list.stream().sorted((o1, o2) -> ApplicationUtils.compareAppVersion(o2.getAppVersion(), o1.getAppVersion())).collect(Collectors.toList());

    将对象的集合,按照某个属性,进行分组

    Map<String, List<YourEntity>> mapByOne = list.stream()
    .collect(Collectors.groupingBy(YourEntity::getName));

    将对象的集合,按照数值和时间的降序,进行排序

    List<YourEntity> afterList = list.stream()
    .sorted(
        Comparator
            .comparing(YourEntity::getLevelValue).reversed()
            .thenComparing(YourEntity::getUpdateTime).reversed()
    )
    .collect(Collectors.toList());

    将对象的集合,按照数值和时间的升序,进行排序

    List<YourEntity> afterList = list.stream()
    .sorted(
        Comparator
            .comparing(YourEntity::getLevelValue)
            .thenComparing(YourEntity::getUpdateTime)
    )
    .collect(Collectors.toList());

    将对象的集合,按照指定KEY-Value,获取最大值Map集合

    例如:根据名字和性别作为唯一Key,找出相同年龄最大的同学的集合
    Map<String, YourEntity> filters = list.stream().collect(
        Collectors.toMap(o -> o.getName() + o.getSex(),
                         Function.identity(),
                         BinaryOperator.maxBy((o1, o2) ->CompareUtils.compareAppVersion(o1.getAge(), o2.getAge()))
                        )
    );

    将对象的集合,按照某个属性值,得到该属性的集合

    List<String> newList = list.stream().map(YourEntity::getName).collect(Collectors.toList());

    将对象的集合,按照某个属性值,用“,”进行拼接为字符串

    String str = list.stream().map(YourEntity::getName).collect(Collectors.joining(","))

    将对象的集合,用“,”进行拼接为字符串

    String str = list.stream().map(Object::toString).collect(Collectors.joining(","))

    将对象的集合,按照某个属性,进行分组

    Map<String, List<YourEntity>> collect = list.stream().collect(Collectors.groupingBy(YourEntity::getName));

    集合1中,根据某些属性值,排除集合2,去掉差集

    list1.stream().filter(
        f -> list2.stream().noneMatch(m -> (m.getPackageName() +
                                            m.getAppType()).equals(f.getPackageName() + f.getAppType())))
    .collect(Collectors.toList());

    根据用户名分组求用户余额总和,然后按照总和进行最大值排序

    Map<String, Integer> calculate = Maps.newLinkedHashMap();
    Map<String, Integer> collect = list.stream().collect(Collectors.groupingBy(User::getName, Collectors.summingInt(User::getMoney)));
    collect.entrySet().stream().sorted(Map.Entry.<String, Integer>comparingByValue().reversed()).forEachOrdered(e -> calculate.put(e.getKey(), e.getValue()));