java对list中的数据进行去重
原创    bluesky    发表于:2018-01-04 15:21:40
  阅读 :101   收藏   编辑

概述:

  • hashset是如何保持元素的唯一性呢?

是通过元素的hashcode和equals来表示:

  1. 如果hashCode值一样,则比较equals是否为true

  2. 如果hashCode值不一样,不用比较equals

  • List是如何集合中元素相同的呢?

是通过元素的hashcode和equals来表示:

  1. 如果hashCode值一样,则比较equals是否为true

  2. 如果hashCode值不一样,不用比较equals

简单list

public static void main(String[] args) {


        List<String> source = new ArrayList<String>();
        source.add("1");
        source.add("2");
        source.add("3");
        source.add("3");
        source.add("4");


        Set set = new HashSet();
        List target  = new  ArrayList(); 
        for (String item : source) {
           if(set.add(item)){
               target.add(item);
           }
       }

       System.err.println("去重后的list-> " + target);

}

复杂list,重写hashCode,equals

package test;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class JavaList {

    class Bean {

        private Integer age;
        private String name;


        public Bean(Integer age, String name) {
            super();
            this.age = age;
            this.name = name;
        }

        public Integer getAge() {
            return age;
        }

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

        public String getName() {
            return name;
        }

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


        @Override
        public int hashCode() {
            final int prime = 31;
            int result = 1;
            result = prime * result + ((age == null) ? 0 : age.hashCode());
            result = prime * result + ((name == null) ? 0 : name.hashCode());
            return result;
        }


        @Override
        public boolean equals(Object obj) {
            if (this == obj){
                return true;
            }
            if (obj == null){
                return false;
            }

            Bean other = (Bean) obj;
            if (age == null) {
                if (other.age != null)
                    return false;
            } else if (!age.equals(other.age))
                return false;
            if (name == null) {
                if (other.name != null)
                    return false;
            } else if (!name.equals(other.name))
                return false;
            return true;
        }

        @Override
        public String toString() {
            return "Bean [age=" + age + ", name=" + name + "]";
        }


    }

    public static void main(String[] args) {
        JavaList  javaList = new JavaList();
        //复杂list
        List<Bean> source = new ArrayList<Bean>();
        source.add(javaList.new Bean(1, "A"));
        source.add(javaList.new Bean(2, "B"));
        source.add(javaList.new Bean(3, "C"));
        source.add(javaList.new Bean(3, "C"));
        source.add(javaList.new Bean(4, "D"));

        Set set = new HashSet();
        List target = new ArrayList();
        for (Bean item : source) {
            if (set.add(item)) {
                target.add(item);
            }
        }

        System.err.println("去重后的list-> " + target);

    }

}
评论
条评论