• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Finding "equal" elements in a List.

 
Landon Blake
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the easiest way to find elements with the same Value in a List? Is there a utility class that allows you to determine if a list contains duplicate "values"? How about the number of duplicate values?

Thanks,

Landon
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34669
366
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Landon,
The easiest thing I can think of is to put all the objects in a HashSet and then compare the number of elements in the set to the number of elements in a list. The set takes care of filtering duplicates. And the difference in size would be the number of duplicates.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alternately, if you want to know the number of duplicates for each element, you can use something like the MultiCounter I posted here. You could add methods to do things like find the most common element, or give a Set of all the keys.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic