• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

making list as unique

 
Ranch Hand
Posts: 156
Hibernate Eclipse IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a list which contains Employees objects.
the Employee objects have following attributs

name
number

I want to make this list as unique.

for example my EmployeeList contains three objects
name number
Employee1 1
Employee2 2
Employee1 1


the resulting list should contain the employee objects with unqie employees

the resulting List should be

name number
EMployee1 1
Employee2 2
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In this case the most natural thing is to overwrite the equals method in your Employee class and use a Set<Employee> not a List.
According to the definition in the JavaDoc a "Set" interface is

A collection that contains no duplicate elements. More formally, sets contain no pair of elements e1 and e2 such that e1.equals(e2), and at most one null element.



Choose the implelentation you preafer (or better that fit best your requirements) beetween HashSet, LinkedHashSet, TreeSet.

Regards
Maurizio
 
CLUCK LIKE A CHICKEN! Now look at this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic