如何避免在Java中将重复数据添加到类Arraylist中

I am trying to add new object of Person class into arraylist. But I should avoid adding duplicate data with only one attribute of Person class. To be more precise Every Person has unique clientId. I need to check same clientIds should not be added into arraylist. When I try to add new Person Info with same clientId arraylist should just skip it. It does not matter other attributes are same except clientId. I am trying to check without looping Arraylist. I need more better way. I tried to use Set, But Set checks whole object. Any help is appreciated.

public class Person {

    private String clientId;
    private Integer age;
    private String name;

    public Person() {
    }


    public Person(String clientId, Integer age, String name) {
        this.clientId = clientId;
        this.age = age;
        this.name = name;
    }



    public String getClientId() {
        return clientId;
    }

    public void setClientId(String clientId) {
        this.clientId = clientId;
    }

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

}

public class ManagePeople {


    public static void main(String[] args) {

        ArrayList<Person> perArrayList = new ArrayList<Person>();

        Person person;
        perArrayList.add(new Person("12", 100, "Tom"));
        perArrayList.add(new Person("14", 124, "Harold"));
        perArrayList.add(new Person("13", 234, "Petty"));
        perArrayList.add(new Person("15", 244, "Pedro"));
        perArrayList.add(new Person("16", 125, "Harry"));

    }

}

评论
jsed
jsed

使用HashMap和客户端ID作为键,并将值用作您的Person对象。地图不允许添加重复键。

HashMap<String, Person> tne = new HashMap<String, Person>();
点赞
评论
贪婪的想念
贪婪的想念

您需要执行以下步骤:

  1. Override equals() method in Person to return true when the clientId is same.

    class Person {
        private String clientId;
        // Other attributes
    
        @Override
        public int hashCode() {
            return Objects.hash(clientId, age, name);
        }
    
        @Override
        public boolean equals(Object obj) {
            Person other = (Person) obj;
            return this.clientId.equals(other.clientId);
        }
    
        // Constructor(s), getters, setters and other methods
    
    }
    
  2. Use a Set instead of ArrayList. The set will automatically discard the duplicate record at the time of adding.

    Set<Person> personSet = new HashSet<Person>();
    personSet.add(new Person("12", 100, "Tom"));
    personSet.add(new Person("14", 124, "Harold"));
    personSet.add(new Person("13", 234, "Petty"));
    personSet.add(new Person("15", 244, "Pedro"));
    personSet.add(new Person("16", 125, "Harry"));
    
点赞
评论
Ctrl
Ctrl

使用Set而不是ArrayList,因为Set只能存储唯一的对象,因此可以避免重复。

Set<Person> perArrayList = new HashSet<>();

Person person;
perArrayList.add(new Person("12", 100, "Tom"));
perArrayList.add(new Person("14", 124, "Harold"));
perArrayList.add(new Person("13", 234, "Petty"));
perArrayList.add(new Person("15", 244, "Pedro"));
perArrayList.add(new Person("16", 125, "Harry"));
点赞
评论