create unique list of objecs not working as expected [closed]

4 days ago 5
ARTICLE AD BOX

I am trying to create an unique list of objects by casting them into a HashSet

e.g.

ArrayList<Person> deciders = new ArrayList<Person>(); Person jane= new Person(); jane.setName(""); jane.setPhone(""); jane.setEmail(""); jane.setId("Jane123"); deciders.add(jane); Person ja_ne= new Person(); ja_ne.setName(""); ja_ne.setPhone(""); ja_ne.setEmail(""); ja_ne.setId("Jane789"); deciders.add(ja_ne); Person john= new Person(); john.setName(""); john.setPhone(""); john.setEmail(""); john.setId("John456"); deciders.add(john); Set<Person> uniqueElements = new HashSet<Person>(deciders); deciders.clear(); deciders.addAll(uniqueElements);

why does uniqueElements contain only Jane and not John or Ja_ne? Do the fields in Person object have to have unique values?

What other simple options do I have to create a unique list of objects?

Malin's user avatar

2

Read Entire Article