Lambda表达式去重

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;
    }

    /**
     * 1.选用Lambda表达简洁,运行效率更高,其实还是利用了map的特性进行去重
     * 2.distinctByKey()方法,讲解下这个方法里的参数内容
     * (1)首先是Predicate这个返回参数,传入参数,返回true/false,(可以翻看源码看下)
     * (2)对于Lambda表达式来说 filter会过滤为false的值,而参数Function是用来代替所传参数(可以翻看源码)
     * (3)创建一个ConcurrentHashMap,是由于流的读取是基于并发的,这个map集合在基于线程安全的情况下,并发效率更高
     * (4)putIfAbsent()使用了map源码中的方法,大意是 当键不存在时,把参数放入map中,并返回null,如果键存在,则返回false,
     *   所以当键存不存在时为null,null==null为true,当键存在时,则返回false,这样就做到了 相同数据的筛选
     */

    //根据对象的某个原属去重后的 对象集合
    //[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;
    }

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值