Bookmark Topic Watch Topic
  • New Topic

How to store unique elements in List?

 
Rahul Noel
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • 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.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • 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 ]
 
karimkhan pathan
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Report post to moderator
Hi ,

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

Regards,
karim.
 
Rj Vasanth kumar
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • 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
  • Report post to moderator
Thanks a lot vasanth ,

But i need duplicates to be stored in another List.

I got it ...Thanks all ..
 
Rob Spoor
Sheriff
Pie
Posts: 20671
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Report post to moderator
Please don't hijack threads. Your problem may be related to the original problem, but it is definitely different.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50278
80
  • Mark post as helpful
  • send pies
  • Report post to moderator
Rob Prime wrote:Please don't hijack threads. . . .
Agree. Closing thread.
 
    Bookmark Topic Watch Topic
  • New Topic