Hi all,
can any one tell me how to find unique values in array list?
Thanks in advance..
anand01
0
Junior Poster
Recommended Answers
Jump to PostWhat kind of values or object are you talking about? Do you have a compare method to work with? There are various ways to deal with it, but it depends on what object you are storing in your array list and how they can be compared.
Jump to PostYou need to tell us the type of values you have in the `List'. Assuming you have simple strings in the list, you can do:
// untested final List<String> lst = Arrays.asList("a", "b", "c", "a", "c", "b"); final Set<String> set = new HashSet<String>(lst); System.out.printf("Original list: %s%n", lst); …
Jump to PostRight, because Set guarantees unique elements, not necessarily in sorted order. If you need to maintain the order of elements, use `TreeSet<String>`. Just make sure the objects you insert in the TreeSet implement natural ordering (by implementing Comparable) or be ready to provide your own custom ordering (implement Comparator). Anyways, …
All 10 Replies
Taywin
312
Posting Virtuoso
~s.o.s~
2,560
Failure as a human
Team Colleague
Featured Poster
anand01
0
Junior Poster
~s.o.s~
2,560
Failure as a human
Team Colleague
Featured Poster
mKorbel
274
Veteran Poster
peter_budo
2,532
Code tags enforcer
Team Colleague
Featured Poster
anand01
0
Junior Poster
anand01
0
Junior Poster
~s.o.s~
2,560
Failure as a human
Team Colleague
Featured Poster
anand01
0
Junior Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.