非常全面的lambda表达式对List集合去重、分组、排序、过滤、求和、最值方法工具类

我们经常会对list集合去重,于是我自己花了点时间将如何用 lambda表达式对list集合去重做了封装和测试,方便以后快速使用。包括对对象集合中的某个属性去重等。如果有特殊的list去重要求,可以自行对下列方法进行修改。

下列方法是本人验证过比较简便的写法,请参考:

一、取值、分组

1、取值

 // 获取list中所有对象的某个属性
        List<String> list= cities.stream().map(City :: getCity).collect(Collectors.toList());
        List<String> list= cities.stream().map(o->o.getCity()).collect(Collectors.toList());

2、分组

        Map<String, List<City>> map =  cities.stream().collect(Collectors.groupingBy(City :: getCity));

如果报错误:element cannot be mapped to a null key
则加上非空过滤:

        Map<String, List<City>> map =  cities.stream().filter(v -> StringUtils.isNotEmpty(v.getCity())).collect(Collectors.groupingBy(City :: getCity));

二、去重

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

public class ListUtil {

    public static List<String> list = Arrays.asList("1","2","2","3","3","4","4","5","6","7","8");
    public static List<City> cities = null;
    static {
        cities = new ArrayList<City>(){
            {
                add(new City("上海",11));
                add(new City("武汉",22));
                add(new City("武汉",55));
                add(new City("上海",33));
                add(new City("北京",33));
                add(new City("深圳",43));
            }
        };

    }
    public static void main(String[] args) {
        System.out.println(ListUtil.distinctElements(list));
        System.out.println(ListUtil.getNoDuplicateElements(list));
        System.out.println(ListUtil.getDuplicateElements(list));
        System.out.println(ListUtil.getDuplicateElementsForObject(cities));
        System.out.println(ListUtil.getNoDuplicateElementsForObject(cities));
        System.out.println(ListUtil.getElementsAfterDuplicate(cities));
        System.out.println(ListUtil.getDuplicateObject(cities));
        System.out.println(ListUtil.getNoDuplicateObject(cities));
        System.out.println(ListUtil.distinctObject(cities));
    }



    //去重后的集合 [1, 2, 3, 4, 5, 6, 7, 8]
    public static <T> List<T> distinctElements(List<T> list) {
        return list.stream().distinct().collect(Collectors.toList());
    }

    //lambda表达式 去除集合重复的值  [1, 5, 6, 7, 8]
    public static <T> List<T> getNoDuplicateElements(List<T> list) {
        // 获得元素出现频率的 Map,键为元素,值为元素出现的次数
        Map<T, Long> map = list.stream().collect(Collectors.groupingBy(p -> p,Collectors.counting()));
        System.out.println("getDuplicateElements2: "+map);
        return map.entrySet().stream() // Set<Entry>转换为Stream<Entry>
                .filter(entry -> entry.getValue() == 1) // 过滤出元素出现次数等于 1 的 entry
                .map(entry -> entry.getKey()) // 获得 entry 的键(重复元素)对应的 Stream
                .collect(Collectors.toList()); // 转化为 List
    }

    //lambda表达式 查找出重复的集合 [2, 3, 4]
    public static <T> List<T> getDuplicateElements(List<T> list) {
        return list.stream().collect(Collectors.collectingAndThen(Collectors
                            .groupingBy(p -> p, Collectors.counting()), map->{
                                map.values().removeIf(size -> size ==1); // >1 查找不重复的集合;== 1 查找重复的集合
                                List<T> tempList = new ArrayList<>(map.keySet());
                                return tempList;
                            }));
    }

    //利用set集合
    public static <T> Set<T> getDuplicateElements2(List<T> list) {
        Set<T> set = new HashSet<>();
        Set<T> exist = new HashSet<>();
        for (T s : list) {
            if (set.contains(s)) {
                exist.add(s);
            } else {
                set.add(s);
            }
        }
        return exist;
    }

    /**-----------对象List做处理--------------*/

    //查找对象中某个原属重复的  属性集合   [上海, 武汉]
    public static List<String> getDuplicateElementsForObject(List<City> list) {
        return list.stream().collect(Collectors.groupingBy(p -> p.getCity(),Collectors.counting())).entrySet().stream()
                .filter(entry -> entry.getValue() > 1) // >1 查找重复的集合;== 查找不重复的集合
                .map(entry -> entry.getKey())
                .collect(Collectors.toList());
    }

    //查找对象中某个原属未重复的  属性集合   [深圳, 北京]
    public static List<String> getNoDuplicateElementsForObject(List<City> list){
        Map<String,List<City>> map = list.stream().collect(Collectors.groupingBy(City::getCity));
        return map.entrySet().stream().filter(entry -> entry.getValue().size() == 1)
                .map(entry -> entry.getKey()) // 获得 entry 的键(重复元素)对应的 Stream
                .collect(Collectors.toList()); // 转化为 List

    }

    //查找对象中某个原属去重后的集合 [上海, 武汉, 北京, 深圳]
    public static List<String> getElementsAfterDuplicate(List<City> list) {
        return list.stream().map(o->o.getCity()).distinct().collect(Collectors.toList());
    }

    //对象中某个原属重复的 对象集合
    // [[City(city=上海, total=11), City(city=上海, total=33)], [City(city=武汉, total=22), City(city=武汉, total=55)]]
    public static List<List<City>> getDuplicateObject(List<City> list) {
        return list.stream().collect(Collectors.groupingBy(City::getCity)).entrySet().stream()
                .filter(entry -> entry.getValue().size() > 1) // >1 查找重复的集合;== 查找不重复的集合
                .map(entry -> entry.getValue())
                .collect(Collectors.toList());
    }

    //对象中某个原属未重复 对象集合
    //[[City(city=深圳, total=43)], [City(city=北京, total=33)]]
    public static List<City> getNoDuplicateObject(List<City> list) {
        List<City> cities = new ArrayList<>();
        list.stream().collect(Collectors.groupingBy(City::getCity)).entrySet().stream()
                    .filter(entry -> entry.getValue().size() ==1) //>1 查找重复的集合;== 查找不重复的集合;
                    .map(entry -> entry.getValue())
                    .forEach(p -> cities.addAll(p));
        return cities;
    }


    //根据对象的某个原属去重后的 对象集合
    //[City(city=上海, total=11), City(city=武汉, total=22), City(city=北京, total=33), City(city=深圳, total=43)]
    public static List<City> distinctObject(List<City> list) {
        return list.stream().filter(distinctByKey(City::getCity)).collect(Collectors.toList());
    }

    public static <T> Predicate<T> distinctByKey(Function<? super T, Object> keyExtractor) {
        Map<Object, Boolean> seen = new ConcurrentHashMap<>();
        return object -> seen.putIfAbsent(keyExtractor.apply(object), Boolean.TRUE) == null;
    }

}



三、排序

import java.util.*;
import java.util.stream.Collectors;

public class ListUtil_sort {

    public static List<Integer> list = Arrays.asList(10,1,6,4,8,7,9,3,2,5);
    public static List<City> cities = null;
    public static List<City> cities2 = null;
    static {
        cities = new ArrayList<City>(){
            {
                add(new City("上海",11));
                add(new City("武汉",55));
                add(new City("南京",33));
                add(new City("深圳",33));
            }
        };

        cities2 = new ArrayList<City>(){
            {
                add(new City("上海",11,11));
                add(new City("武汉",55,22));
                add(new City("南京",33,55));
                add(new City("深圳",33,44));
            }
        };

    }
    public static void main(String[] args) {
        System.out.println(sort(list));
        System.out.println(reversed(list));
        System.out.println(sortForObject(cities));
        System.out.println(reversedForObject(cities));
        System.out.println(sortForObject2(cities2));
    }

    //list排序 正序
    public static <T> List<T> sort(List<T> list){
        return list.stream().sorted().collect(Collectors.toList());
    }

    //list排序 倒序
    public static List<Integer> reversed(List<Integer> list){
        return list.stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList());
    }

    //根据对象某个属性排序  正序
    public static List<City> sortForObject(List<City> list){
        return list.stream().sorted(Comparator.comparing(City::getTotal)).collect(Collectors.toList());
    }

    //根据对象某个属性排序  倒序
    public static List<City> reversedForObject(List<City> list){
        return list.stream().sorted(Comparator.comparing(City::getTotal).reversed()).collect(Collectors.toList());
    }

    //根据对象两个属性排序  正序
    public static List<City> sortForObject2(List<City> list){
        return list.stream().sorted(Comparator.comparing(City::getTotal).thenComparing(City::getNum)).collect(Collectors.toList());
    }
}

四、过滤、求和、最值

import java.util.*;
import java.util.stream.Collectors;

public class ListUtil_sum {

    public static List<Integer> list = Arrays.asList(10,1,6,4,8,7,9,3,2,5);
    public static List<String> strList = Arrays.asList("10","1","6","4");
    public static List<City> cities = null;
    public static Map<String,Integer> cityMap = null;
    static {
        cities = new ArrayList<City>(){
            {
                add(new City("上海",11));
                add(new City("武汉",55));
                add(new City("武汉",45));
                add(new City("深圳",33));
            }
        };
        cityMap = new HashMap<>();
        cityMap.put("武汉",55);
        cityMap.put("上海",11);
    }
    public static void main(String[] args) {
        System.out.println(calculation(list));
        calculation2(cities);
        listToMap(cities);
        mapToList(cityMap);
        stringToList("上海、武汉");
        joinStringValueByList(cities);
        joinStringValueByList2(strList);
        System.out.println(filter(cities));
    }


    //根据对象某个属性求各自值
    ///IntSummaryStatistics{count=4, sum=132, min=11, average=33.000000, max=55}
    public static IntSummaryStatistics calculation(List<Integer> list){
        IntSummaryStatistics stat = list.stream().collect(Collectors.summarizingInt(p -> p));
        System.out.println("max:"+stat.getMax());
        System.out.println("min:"+stat.getMin());
        System.out.println("sum:"+stat.getSum());
        System.out.println("count:"+stat.getCount());
        System.out.println("average:"+stat.getAverage());
        Integer max = list.stream().reduce(Integer::max).get();//得到最大值
        Integer min = list.stream().reduce(Integer::min).get();//得到最小值
        System.out.println("max:"+max+";min:"+min);
        return stat;
    }
    //根据对象某个属性求各自值
    public static void calculation2(List<City> list){
        System.out.println("sum="+ list.stream().mapToInt(City::getTotal).sum());
        System.out.println("max="+ list.stream().mapToInt(City::getTotal).max().getAsInt());
        System.out.println("min="+ list.stream().mapToInt(City::getTotal).min().getAsInt());
        System.out.println("ave="+ list.stream().mapToInt(City::getTotal).average().getAsDouble());
    }

    //功能描述 List转map
    public static void listToMap(List<City> list){
        //用 (k1,k2)->k1 来设置,如果有重复的key,则保留key1,舍弃key2
        Map<String,City> map = list.stream().collect(Collectors.toMap(City::getCity,city -> city, (k1, k2) -> k1));
        map.forEach((k,v) -> System.out.println("k=" + k + ",v=" + v));
    }


    //对象某个属性 等于特定值的累加
    public static void calculation11(List<City> list){
        Map<String, IntSummaryStatistics> intSummaryStatistics = list.stream().
                collect(Collectors.groupingBy(i -> i.getCity(), Collectors.summarizingInt(City::getTotal)));
        System.out.println("-4-->" + intSummaryStatistics);
        System.out.println("-5-->" + intSummaryStatistics.get("武汉").getSum());
    }

    //功能描述 map转list
    public static void mapToList(Map<String,Integer> map){
        List<City> list = map.entrySet().stream().map(key -> new City(key.getKey(),key.getValue())).collect(Collectors.toList());
        System.out.println(list);
        list.forEach(bean -> System.out.println(bean.getCity() + "," + bean.getTotal()));
    }

    //功能描述 字符串转list
    public static void stringToList(String str){
        //不需要处理
        //<String> list = Arrays.asList(str.split(","));
        //需要处理
        List<String> list = Arrays.asList(str.split(",")).stream().map(string -> String.valueOf(string)).collect(Collectors.toList());
        list.forEach(string -> System.out.println(string));
    }

    //功能描述 姓名以逗号拼接
    public static void joinStringValueByList(List<City> list){
        System.out.println(list.stream().map(City::getCity).collect(Collectors.joining(",")));
    }

    //功能描述 姓名以逗号拼接
    public static void joinStringValueByList2(List<String> list){
        //方式一
        System.out.println(String.join(",", list));
        //方式二
        System.out.println(list.stream().collect(Collectors.joining(",")));
    }

    //功能描述 过滤
    public static List<City> filter(List<City> list){
        return list.stream().filter(a -> a.getTotal()>44).collect(Collectors.toList());
    }

}




将List集合求和:

    public static void main(String[] args) {
        List<BigDecimal> list = new ArrayList<>();
        list.add(BigDecimal.valueOf(1.1));
        list.add(BigDecimal.valueOf(1.2));
        list.add(BigDecimal.valueOf(1.3));
        list.add(BigDecimal.valueOf(1.4));
 
        BigDecimal decimal = list.stream().reduce(BigDecimal.ZERO, BigDecimal::add);
        System.out.println(decimal);
    }

五、List对象转换

@Data
public class CityDTO {
    private String city;
    private Integer total;
    private Integer num;

    public CityDTO(){}

    public CityDTO(CityDO doo){
        this.city =doo.getCity();
        this.total =doo.getTotal();
    }

    public static void main(String[] args) {
        List<CityDO> dos = Arrays.asList(new CityDO("上海",11),new CityDO("武汉",22));
        List<CityDTO> dtos = dos.stream().map(CityDTO::new).collect(Collectors.toList());
    }

六、map遍历,map转list,list转map

1、遍历map

map.forEach((k, v) -> System.out.println("key:value = " + k + ":" + v));

2、map转list

map.entrySet().stream().map(e -> new Person(e.getKey(),e.getValue())).collect(Collectors.toList());
map.keySet().stream().collect(Collectors.toList());
map.values().stream().collect(Collectors.toList());

3、list转map

 Map<Integer, String> result1 = list.stream().collect(
                Collectors.toMap(Hosting::getId, Hosting::getName));

Map<Long, User> maps = userList.stream().collect(Collectors.toMap(User::getId,Function.identity()));

//看来还是使用JDK 1.8方便一些。另外,转换成map的时候,可能出现key一样的情况,如果不指定一个覆盖规则,上面的代码是会报错的。转成map的时候,最好使用下面的方式
Map<Long, User> maps = userList.stream().collect(Collectors.toMap(User::getId, Function.identity(), (key1, key2) -> key2));

七、对list对某一属性相同去重,保留其他属性值最大的对象

假设有一个对象类 Obj,其中有属性 id、name、value,需要对 id 相同的对象进行去重,保留 value 最大的对象。可以使用 Java8 的流和 lambda 表达式完成这个操作:

List<Obj> list; // 初始化 list
List<Obj> result = list.stream()
    .collect(Collectors.toMap(Obj::getId, Function.identity(), (obj1, obj2) -> obj1.getValue() > obj2.getValue() ? obj1 : obj2))
    .values().stream().collect(Collectors.toList());

上述代码中,首先将 list 转化为 Map,以 id 为键、Obj 对象为值,如果遇到重复的键时使用 merge 函数将其 value 值较大的对象作为值;然后获取 Map 的值集合并转化为 List,即为去重后的结果。

  • 25
    点赞
  • 126
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Java 8 的 streamlambda 表达式提供了非常方便的操作 list 的方式,下面是通过 stream 去重的示例代码: ```java List<Integer> list = Arrays.asList(1, 2, 3, 2, 4, 1); List<Integer> distinctList = list.stream().distinct().collect(Collectors.toList()); System.out.println(distinctList); // 输出 [1, 2, 3, 4] ``` 上述代码中,我们首先创建了一个包含重复元素的 list,然后通过 stream() 方法将其转换为 Stream 对象,接着调用 distinct() 方法去重,最后用 collect(Collectors.toList()) 方法Stream 转换为 List。 除了去重stream 还提供了很多其他的操作,如分组过滤求和最值排序等等。下面是一些示例代码: ```java // 分组 Map<Boolean, List<Integer>> groupByEven = list.stream().collect(Collectors.partitioningBy(i -> i % 2 == 0)); System.out.println(groupByEven); // 输出 {false=[1, 3, 1], true=[2, 2, 4]} // 过滤 List<Integer> filteredList = list.stream().filter(i -> i > 2).collect(Collectors.toList()); System.out.println(filteredList); // 输出 [3, 4] // 求和 int sum = list.stream().mapToInt(Integer::intValue).sum(); System.out.println(sum); // 输出 13 // 最值 Optional<Integer> max = list.stream().max(Integer::compare); System.out.println(max.get()); // 输出 4 // 排序 List<Integer> sortedList = list.stream().sorted().collect(Collectors.toList()); System.out.println(sortedList); // 输出 [1, 1, 2, 2, 3, 4] // 去重 List<Integer> distinctList2 = list.stream().distinct().collect(Collectors.toList()); System.out.println(distinctList2); // 输出 [1, 2, 3, 4] ``` 上述代码中,我们使用 partitioningBy() 方法list 分成奇数和偶数两组;使用 filter() 方法过滤出大于 2 的元素;使用 mapToInt() 方法将 Integer 转换为 int,再使用 sum() 方法求和;使用 max() 方法找出最大值;使用 sorted() 方法排序;使用 distinct() 方法去重。注意,使用 distinct() 方法去重时,需要保证元素的类型实现了 equals() 和 hashCode() 方法,否则可能会出现意外的结果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值