Monday, 25 April 2016

How to find unique values in ArrayList ?


To find unique values in ArrayList , simplest is to convert ArrayList to Set

Assuming your items of the `List' implement an appropriate equals / hashCode() method or the Comparable interface

Example
List<String> list = Arrays.asList("a", "b", "c", "a", "c", "b");
Set<String> set = new HashSet<String>(list);
System.out.printf("Original list : " + list);

System.out.printf("Unique values : " + set);

No comments:

Post a Comment

Note: only a member of this blog may post a comment.