• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic