In this Q&A, we'll go over three options to remove duplicates from Array List. All three options create a new List.
Using stream
One can remove duplicates using the stream.distinct(). This is a stable distinct and order of elements in the List is maintained. See distinctWithStream() method below
Using HashSet or LinkedHashSet
Create a HashSet from List and convert set back to list. Order of elements in the resultant list is not guaranteed to be the same as in source. See distinctWithHashSet() method below
To maintain order use LinkedHashSet instead of HashSet. See distinctWithLinkedHashSet() method below
import java.util.*;import java.util.stream.Collectors;
import static org.junit.Assert.assertEquals;
public class Distinct {
public static void distinctWithStream() {
List<Integer> list = Arrays.asList(100, 100, 200, 300, 300, 400, 500, 600);
// Displaying the distinct elements in the list
// using Stream.distinct() method
List<Integer> l2 = list.stream().distinct().collect(Collectors.toList());
List<Integer> l3 = Arrays.asList(100, 200, 300, 400, 500, 600);
assertEquals("stream distinct ", l2 ,l3);
}
public static void distinctWithHashSet() {
List<Integer> list = Arrays.asList(100, 100, 200, 300, 300, 400, 500, 600);
List<Integer> l2 = Arrays.asList(100, 200, 300, 400, 500, 600);
HashSet<Integer> si = new HashSet<>(list);
List<Integer> l4 = new ArrayList<Integer>(si);
System.out.println( l4.size() == l2.size() ? "Size matches" : "Size does not match" );
System.out.println( !l2.equals(l4) ? "Order not maintained " : "Order maintained " );
}
public static void distinctWithLinkedHashSet() {
List<Integer> list = Arrays.asList(100, 100, 200, 300, 300, 400, 500, 600);
List<Integer> l2 = Arrays.asList(100, 200, 300, 400, 500, 600);
HashSet<Integer> si = new LinkedHashSet<>(list);
List<Integer> l4 = new ArrayList<Integer>(si);
System.out.println( l4.size() == l2.size() ? "Size matches" : "Size does not match" );
System.out.println( !l2.equals(l4) ? "Order not maintained " : "Order maintained " );
}
public static void main( String[] args ) {
distinctWithStream();
distinctWithHashSet();
distinctWithLinkedHashSet();
}
}
Comments
Post a Comment