java List怎么分组去重

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java List怎么分组去重相关的知识,希望对你有一定的参考价值。

有一个List里面存放的是对象,对象是 Sh(hm, credit, year, 1, sessionid),现在要根据year来去除hm的重复数据,要怎么实现?

使用set集合啊,set集合是不能放重复的数据的。
Set hashSet = new HashSet(list);
然后在转过来就行了,set去除重复是调用了equals方法,所以你在Sh里面要重写equals方法,自定义比较的规则,例如:
public boolean equals(Object obj)
if(obj==null) return false;
if(obj==this) return true;
Sh sh = (Sh)obj;
if(sh.getHm()==this.getHm())
return true;

return false;
参考技术A 用set集合吧. 那个是不允许重复的,操作和list一样追问

但是不同年的hm是可以一样的,这要怎么实现

追答

你就在对象中重写equals方法, 判断要同年和hm都一样的才算是相同的对象.

参考技术B 使用 HashSet 重写 equals 方法与hashcode 方法

简单说明下 HashSet 的实现:
把调用add 方法时, 要求每个对象出示其hashcode()方法返回值,检查是否相同,不同则接纳,相同调用equals方法进行比较 ,相同丢弃,不相同接纳
参考技术C 重写Sh的equals方法

java8 新特性 Stream流 分组 排序 过滤 多条件去重

    private static List<User> list = new ArrayList<User>();
    
    public static void main(String[] args) {
        list =  Arrays.asList(
                new User(1, "a", 10),
                new User(4, "d", 19),
                new User(5, "e", 13),
                new User(2, "b", 14),
                new User(3, "a", 10),
                new User(6, "f", 16)
                );
        
        long start = System.currentTimeMillis();
        
        order();
        
        println(String.format("耗时[%s]毫秒", (System.currentTimeMillis() - start)));
        
    }
    
    /**
     * 多条件去重
     * @param list
     */
    public static void order() {
        list.stream().collect(Collectors.collectingAndThen(
                Collectors.toCollection(() -> new TreeSet<>(
                        Comparator.comparing(o -> o.getAge() + ";" + o.getName()))), ArrayList::new)).forEach(u -> println(u));
    }
    
    public static void group() {
        Map<Integer, List<User>> collect = list.stream().collect(Collectors.groupingBy(User::getAge));
        System.out.println(collect);
    }
    
    /**
     * filter过滤
     * @param list
     */
    public static void filterAge() {
        list.stream().filter(u -> u.getAge() == 10).forEach(u -> println(u));
    }
    
    /**
     * sorted排序
     */
    public static void stord() {
        list.stream().sorted(Comparator.comparing(u-> u.getAge())).forEach(u -> println(u));
        
    }
    /**
     * limit方法限制最多返回多少元素
     */
    public static void limit() {
        list.stream().limit(2).forEach(u -> println(u));
    }
    /**
     * 不要前多n个元素,n大于满足条件的元素个数就返回空的流
     */
    public static void skip() {
        list.stream().skip(2).forEach(u -> println(u));
    }
    // 最大值 最小值
    public static void statistics() {
        Optional<User> min = list.stream().min(Comparator.comparing(User::getUserId));
        println(min);
        Optional<User> max = list.stream().max(Comparator.comparing(User::getUserId));
        println(max);
    }
    
    // 统计
    public static void summarizingInt(){
        IntSummaryStatistics statistics = list.stream().collect(Collectors.summarizingInt(User::getAge));
        double average = statistics.getAverage();
        long count = statistics.getCount();
        int max = statistics.getMax();
        int min = statistics.getMin();
        long sum = statistics.getSum();
        println(average);
        println(count);
        println(min);
        println(sum);
        println(max);
        
    }
    /**
     * 转set
     */
    public static void toSet() {
        Set<User> collect = list.stream().collect(Collectors.toSet());
        Iterator<User> iterator = collect.iterator();
        while(iterator.hasNext()) {
            System.out.println(iterator.next().getUserId());
        }
    }

    /**
     * 转map
     */
    public static void toMap() {
        Map<Integer, User> collect = list.stream().collect(Collectors.toMap(User::getUserId, u -> u));
        for (Integer in : collect.keySet()) {
          User u = collect.get(in);//得到每个key多对用value的值
          println(u);
        }
    }
    /**
     *map
     */
    public static void map() {
        list.stream().map(User::getUserId).forEach(userId -> println(userId));
        list.stream().mapToInt(User::getAge).forEach(userId -> println(userId));
        list.stream().mapToDouble(User::getUserId).forEach(userId -> println(userId));
        list.stream().mapToLong(User::getUserId).forEach(userId -> println(userId));
    }
    
    /**
     * 查找与匹配
     * allMatch方法与anyMatch差不多,表示所有的元素都满足才返回true。noneMatch方法表示没有元素满足
     */
    public static void anyMatch() {
        boolean anyMatch = list.stream().anyMatch(u -> u.getAge() == 100);
        boolean allMatch = list.stream().allMatch(u -> u.getUserId() == 10);
        boolean noneMatch = list.stream().noneMatch(u -> u.getUserId() == 10);
        println(anyMatch);
        println(allMatch);
        println(noneMatch);
    }
    
    /**
     * reduce操作
     */
    public static void reduce() {
        Optional<Integer> sum = list.stream().map(User::getAge).reduce(Integer::sum);
        Optional<Integer> max = list.stream().map(User::getAge).reduce(Integer::max);
        Optional<Integer> min = list.stream().map(User::getAge).reduce(Integer::min);
        println(sum);
        println(max);
        println(min);
    }
    /**
     * 公共输出
     * @param c
     */
    public static void println(Object c) {
        System.out.println(c.toString());
    }
    

 

private static List<User> list = new ArrayList<User>();public static void main(String[] args) {list =  Arrays.asList(new User(1, "a", 10),new User(4, "d", 19),new User(5, "e", 13),new User(2, "b", 14),new User(3, "a", 10),new User(6, "f", 16));long start = System.currentTimeMillis();order();println(String.format("耗时[%s]毫秒", (System.currentTimeMillis() - start)));}/** * 多条件去重 * @param list */public static void order() {list.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(o -> o.getAge() + ";" + o.getName()))), ArrayList::new)).forEach(u -> println(u));}public static void group() {Map<Integer, List<User>> collect = list.stream().collect(Collectors.groupingBy(User::getAge));System.out.println(collect);}/** * filter过滤 * @param list */public static void filterAge() {list.stream().filter(u -> u.getAge() == 10).forEach(u -> println(u));}/** * sorted排序 */public static void stord() {list.stream().sorted(Comparator.comparing(u-> u.getAge())).forEach(u -> println(u));}/** * limit方法限制最多返回多少元素 */public static void limit() {list.stream().limit(2).forEach(u -> println(u));}/** * 不要前多n个元素,n大于满足条件的元素个数就返回空的流 */public static void skip() {list.stream().skip(2).forEach(u -> println(u));}// 最大值 最小值public static void statistics() {Optional<User> min = list.stream().min(Comparator.comparing(User::getUserId));println(min);Optional<User> max = list.stream().max(Comparator.comparing(User::getUserId));println(max);}// 统计public static void summarizingInt(){IntSummaryStatistics statistics = list.stream().collect(Collectors.summarizingInt(User::getAge));double average = statistics.getAverage();long count = statistics.getCount();int max = statistics.getMax();int min = statistics.getMin();long sum = statistics.getSum();println(average);println(count);println(min);println(sum);println(max);}/** * 转set */public static void toSet() {Set<User> collect = list.stream().collect(Collectors.toSet());Iterator<User> iterator = collect.iterator();while(iterator.hasNext()) {System.out.println(iterator.next().getUserId());}}
/** * 转map */public static void toMap() {Map<Integer, User> collect = list.stream().collect(Collectors.toMap(User::getUserId, u -> u));for (Integer in : collect.keySet()) {  User u = collect.get(in);//得到每个key多对用value的值  println(u);}}/** *map */public static void map() {list.stream().map(User::getUserId).forEach(userId -> println(userId));list.stream().mapToInt(User::getAge).forEach(userId -> println(userId));list.stream().mapToDouble(User::getUserId).forEach(userId -> println(userId));list.stream().mapToLong(User::getUserId).forEach(userId -> println(userId));}/** * 查找与匹配 * allMatch方法与anyMatch差不多,表示所有的元素都满足才返回true。noneMatch方法表示没有元素满足 */public static void anyMatch() {boolean anyMatch = list.stream().anyMatch(u -> u.getAge() == 100);boolean allMatch = list.stream().allMatch(u -> u.getUserId() == 10);boolean noneMatch = list.stream().noneMatch(u -> u.getUserId() == 10);println(anyMatch);println(allMatch);println(noneMatch);}/** * reduce操作 */public static void reduce() {Optional<Integer> sum = list.stream().map(User::getAge).reduce(Integer::sum);Optional<Integer> max = list.stream().map(User::getAge).reduce(Integer::max);Optional<Integer> min = list.stream().map(User::getAge).reduce(Integer::min);println(sum);println(max);println(min);}/** * 公共输出 * @param c */public static void println(Object c) {System.out.println(c.toString());}
--------------------- 作者:anningx 来源:CSDN 原文:https://blog.csdn.net/qq_39739458/article/details/84970293 版权声明:本文为博主原创文章,转载请附上博文链接!

以上是关于java List怎么分组去重的主要内容,如果未能解决你的问题,请参考以下文章

java 8 streamlambda表达式对list操作分组过滤求和最值排序去重

python去重(汉字一样,里面的编码不一样)?

链表Linked List注意事项

java8 list中根据对象多属性去重

java8 新特性 Stream流 分组 排序 过滤 多条件去重

Java爬爬之网页去重和代理ip