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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

How to store unique elements in List?

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Hi,
How to store unique elements in list which already contains duplicate elements in it I mean I want to retrieve unique elements from list where it has duplicate values?


Thanks in Advance,
Rahul Sahu.
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Why use a List if you need to store unique objects? If you have a list with duplicated values in it and you want just the unique values, add the contents of the List to a Set.
[ July 28, 2008: Message edited by: Paul Sturrock ]
 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Hi ,

If a want all the duplicates to be stored in another list ?what I have to do .

Regards,
karim.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Hi
Look this code. Removes the duplicate and returns List object.

org.apache.commons.collections.list.SetUniqueList is from apache common-collection API.

List list =new ArrayList();
list.add("ONE");
list.add("ONE");
list.add("TWO");
list.add("TWO");
Set set = new HashSet();
List l = SetUniqueList.decorate(list);
System.out.println(l);



Regards
vasanth
 
karimkhan pathan
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Thanks a lot vasanth ,

But i need duplicates to be stored in another List.

I got it ...Thanks all ..
 
Sheriff
Posts: 22650
126
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Please don't hijack threads. Your problem may be related to the original problem, but it is definitely different.
 
Marshal
Posts: 75716
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Rob Prime wrote:Please don't hijack threads. . . .

Agree. Closing thread.
 
Listen. That's my theme music. That's how I know I'm a super hero. That, and this tiny ad told me:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
    Bookmark Topic Watch Topic
  • New Topic