美文网首页
java 对象数组去重

java 对象数组去重

作者: 好一个坏小孩 | 来源:发表于2019-07-14 10:56 被阅读0次

    java对象数组去重;

    • 将原数组插入到新数组的时候,将插入的对象和新数组中的已插入对象进行比较,若不相同,则插入到新的数组中;
    • 使用hashSet进行对象的去重(需要将list中对象的hasCode()、equals()方法进行重写,然后放到set集合中)
    package com.arraySort;
    
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Objects;
    import java.util.stream.Collectors;
    
    public class DistinctPerson {
    
        private String name;
    
        private int score;
    
        public DistinctPerson(int score, String name) {
            super();
            this.name = name;
            this.score = score;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getScore() {
            return score;
        }
    
        public void setScore(int score) {
            this.score = score;
        }
    
        /**
         * 当name相同,就认定两个对象相等
         *
         * @param o
         * @return
         */
        @Override
        public boolean equals(Object o) {
            if (this == o) {
                return true;
            }
            if (o == null || getClass() != o.getClass()) {
                return false;
            }
            DistinctPerson that = (DistinctPerson) o;
            return Objects.equals(name, that.name);
        }
    
        /**
         * 仅使用name字段的值来生成hashcode;
         *
         * @return
         */
        @Override
        public int hashCode() {
            return Objects.hash(name);
        }
    
        @Override
        public String toString() {
            return "DistinctPerson{" +
                    "name='" + name + '\'' +
                    ", score=" + score +
                    '}';
        }
    
        private static List<DistinctPerson> initList() {
            List<DistinctPerson> distinctPeople = new ArrayList<DistinctPerson>();
            distinctPeople.add(new DistinctPerson(980, "M1"));
            distinctPeople.add(new DistinctPerson(34, "M2"));
            distinctPeople.add(new DistinctPerson(12, "M3"));
            distinctPeople.add(new DistinctPerson(65, "M4"));
            distinctPeople.add(new DistinctPerson(12, "M5"));
            distinctPeople.add(new DistinctPerson(33, "M6"));
            distinctPeople.add(new DistinctPerson(90, "M7"));
            distinctPeople.add(new DistinctPerson(123, "M8"));
            distinctPeople.add(new DistinctPerson(124, "M8"));
            return distinctPeople;
        }
    
        private static void pirntList(String title, List<DistinctPerson> distinctPeople) {
            System.out.println("==============" + title + "=================");
            for (DistinctPerson distinctPerson : distinctPeople) {
                System.out.println(distinctPerson.toString());
            }
        }
    
        public static void main(String[] args) {
    
    //       方法一:将原数组插入到新数组的时候,将插入的对象和新数组中的已插入对象进行比较,若不相同,则插入到新的数组中;
            List<DistinctPerson> distinctPeople = initList();
            List<DistinctPerson> newDistinctPeople = new ArrayList<>();
            distinctPeople.forEach(t -> {
                //anyMatch表示有一个匹配就返回true;
                boolean match = newDistinctPeople.stream().anyMatch(u -> t.getName().equals(u.getName()));
                if (!match) {
                    newDistinctPeople.add(t);
                }
            });
    
            pirntList("方法一", newDistinctPeople);
    
    
            /**
             方法二:使用hashSet进行对象的去重(需要将list中对象的hasCode()、equals()方法进行重写,然后放到set集合中)
             * 若对象中的equals方法的返回值为true,hashCode方法的返回值必定是相同的;
             */
            distinctPeople = initList();
            distinctPeople = distinctPeople.parallelStream().distinct().collect(Collectors.toList());
            pirntList("方法二", distinctPeople);
        }
    }
    

    运行结果如下:

    ==============方法一=================
    DistinctPerson{name='M1', score=980}
    DistinctPerson{name='M2', score=34}
    DistinctPerson{name='M3', score=12}
    DistinctPerson{name='M4', score=65}
    DistinctPerson{name='M5', score=12}
    DistinctPerson{name='M6', score=33}
    DistinctPerson{name='M7', score=90}
    DistinctPerson{name='M8', score=123}
    ==============方法二=================
    DistinctPerson{name='M1', score=980}
    DistinctPerson{name='M2', score=34}
    DistinctPerson{name='M3', score=12}
    DistinctPerson{name='M4', score=65}
    DistinctPerson{name='M5', score=12}
    DistinctPerson{name='M6', score=33}
    DistinctPerson{name='M7', score=90}
    DistinctPerson{name='M8', score=123}
    

    相关文章

      网友评论

          本文标题:java 对象数组去重

          本文链接:https://www.haomeiwen.com/subject/sshukctx.html