This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes How to store unique elements in List? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply locked New topic
Author

How to store unique elements in List?

Rahul Noel
Ranch Hand

Joined: Jul 18, 2008
Posts: 51
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.


Thanks in Advance.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

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 ]

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
karimkhan pathan
Ranch Hand

Joined: Jul 14, 2008
Posts: 86
Hi ,

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

Regards,
karim.


karim
Rj Vasanth kumar
Greenhorn

Joined: Aug 30, 2009
Posts: 5
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


Vasanth

SCJP 5.0
karimkhan pathan
Ranch Hand

Joined: Jul 14, 2008
Posts: 86
Thanks a lot vasanth ,

But i need duplicates to be stored in another List.

I got it ...Thanks all ..
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

Please don't hijack threads. Your problem may be related to the original problem, but it is definitely different.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38087
    
  22
Rob Prime wrote:Please don't hijack threads. . . .
Agree. Closing thread.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to store unique elements in List?
 
Similar Threads
List and set
Code to get list of duplicate elements from ArrayList
collection doubt again
Dan Exam Q on Collections Doubt?
Set