栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java集合工具类(Java集合转换、去重、过滤、分组)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java集合工具类(Java集合转换、去重、过滤、分组)

Java8中的Lambda+Stream简化了我们对集合类的操作。

通过Lambda+Stream,可以很简洁地实现集合转换、去重、过滤、分组等操作。

以下为封装好的工具类:

package utils;

import java.util.*;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;

public class CollectionKit {
    private CollectionKit() {
    }

    
    public static  List convertToList(Collection collection,
                                               Function mapper) {
        if (collection == null || collection.isEmpty()) {
            return new ArrayList<>();
        }
        return collection.stream().map(mapper).collect(Collectors.toList());
    }

    
    public static  Set convertToSet(Collection collection,
                                             Function mapper) {
        if (collection == null || collection.isEmpty()) {
            return new HashSet<>();
        }
        return collection.stream().map(mapper).collect(Collectors.toSet());
    }

    
    public static  Map convertToMap(Collection collection,
                                                Function keyMapper) {
        if (collection == null || collection.isEmpty()) {
            return new HashMap<>();
        }
        return collection.stream().collect(Collectors.toMap(keyMapper, Function.identity(), (key1, key2) -> key2));
    }

    
    public static  Map convertToMap(Collection collection,
                                                   Function keyMapper,
                                                   Function valueMapper) {
        if (collection == null || collection.isEmpty()) {
            return new HashMap<>();
        }
        return collection.stream().collect(Collectors.toMap(keyMapper, valueMapper, (key1, key2) -> key2));
    }

    
    public static  List filter(Collection collection,
                                     Predicate filterPredicate) {
        if (collection == null || collection.isEmpty()) {
            return new ArrayList<>();
        }
        return collection.stream().filter(filterPredicate).collect(Collectors.toList());
    }

    
    public static  Map> group(Collection collection,
                                               Function classifier) {
        if (collection == null || collection.isEmpty()) {
            return new HashMap<>();
        }
        return collection.stream().collect(Collectors.groupingBy(classifier));
    }

    
    public static  List distinct(Collection collection,
                                       Comparator comparing) {
        return collection.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(comparing)), ArrayList::new));
    }
}

使用详解:

数据准备:

    class User {
        String name;
        int age;

        public String getName() { return name; }

        public void setName(String name) { this.name = name; }

        public int getAge() { return age; }

        public void setAge(int age) { this.age = age; }

        public User(String name, int age) {
            this.name = name;
            this.age = age;
        }
    }

    List users = new ArrayList<>();
    users.add(new User("张三", 10));
    users.add(new User("李四", 10));
    users.add(new User("王五", 20));
    users.add(new User("张三", 25));

1、转换为新的list,每个用户转换为名字+年龄格式的字符串

List list = CollectionKit.convertToList(users, item -> item.name + "_" + item.age);
运行结果:[张三_10, 李四_10, 王五_20, 张三_25]

2、使用convertToSet得到所有用户的年龄(去除重复项)

Set set = CollectionKit.convertToSet(users, User::getAge);
运行结果:[20, 25, 10]

3、使用convertToMap转换为key=name,value=user格式的map

Map map = CollectionKit.convertToMap(users, User::getName);
运行结果:
{
     李四=User{name='李四', age=10},
     张三=User{name='张三', age=25},
     王五=User{name='王五', age=20}
}

4、使用convertToMap转换为key=name,value=age格式的map

Map map1 = CollectionKit.convertToMap(users, User::getName, User::getAge);
运行结果:
{
     李四=10,
     张三=25,
     王五=20
}

5、使用filter筛选年龄为10的用户

List filter = CollectionKit.filter(users, item -> item.age == 10);
运行结果:[User{name='张三', age=10}, User{name='李四', age=10}]

6、使用distinct根据名字或年龄去重

List distinct = CollectionKit.distinct(users, Comparator.comparing(o -> o.name));
List distinct1 = CollectionKit.distinct(users, Comparator.comparing(o -> o.age));
运行结果:
[User{name='张三', age=10}, User{name='李四', age=10}, User{name='王五', age=20}]
[User{name='张三', age=10}, User{name='王五', age=20}, User{name='张三', age=25}]

7、使用group根据年龄分组

Map> group = CollectionKit.group(users, User::getAge);
运行结果:
{
     20=[User{name='王五', age=20}],
     25=[User{name='张三', age=25}],
     10=[User{name='张三', age=10}, User{name='李四', age=10}]
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/843083.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号