Java examples for java.util:List Duplicate Element
Merges the given two lists in a new list, but without duplicates.
//package com.book2s; import java.util.ArrayList; import java.util.Collection; import java.util.List; public class Main { public static void main(String[] argv) { List sourceList1 = java.util.Arrays.asList("asdf", "book2s.com"); List sourceList2 = java.util.Arrays.asList("asdf", "book2s.com"); System.out.println(mergeNoDuplicates(sourceList1, sourceList2)); }//from w ww.ja va 2s . c om /** * Merges the given two lists in a new list, but without duplicates. */ public static <T> List<T> mergeNoDuplicates(List<T> sourceList1, List<T> sourceList2) { List<T> ret = alist(sourceList1.size() + sourceList2.size()); ret.addAll(sourceList1); for (T e : sourceList2) { if (false == ret.contains(e)) ret.add(e); } return ret; } /** * Creates a new empty {@link ArrayList} with the inferred type. */ public static <T> ArrayList<T> alist() { return new ArrayList<T>(); } /** * Creates a new empty {@link ArrayList} with the inferred type * using the given capacity. */ public static <T> ArrayList<T> alist(int initialCapacity) { return new ArrayList<T>(initialCapacity); } /** * Creates a new {@link ArrayList} with the inferred type * using the given elements. */ public static <T> ArrayList<T> alist(Collection<T> vals) { ArrayList<T> ret = new ArrayList<T>(vals.size()); for (T v : vals) ret.add(v); return ret; } /** * Creates a new {@link ArrayList} with the inferred type * using the given elements. */ @SafeVarargs public static <T> ArrayList<T> alist(T... vals) { ArrayList<T> ret = new ArrayList<T>(vals.length); for (T v : vals) ret.add(v); return ret; } /** * Creates a new {@link ArrayList} with the inferred type * using the given elements of all given Collections. */ @SafeVarargs public static <T> ArrayList<T> alist(Collection<T>... lists) { //compute size int count = 0; for (Collection<T> list : lists) count += list.size(); //copy ArrayList<T> ret = new ArrayList<T>(count); for (Collection<T> list : lists) ret.addAll(list); return ret; } /** * Adds the given array elements to the given target list. */ public static <T> void addAll(List<T> target, T[] elements) { for (T e : elements) target.add(e); } }