Java tutorial
//package com.java2s; //License from project: LGPL import java.util.LinkedHashSet; import java.util.List; import java.util.Set; public class Main { /** * The size increase of the created sets and maps to compensate the not full load factor of the maps and sets. */ private static final float SIZE_NORMALIZATION = 1.1f; /** The load factor for maps and sets. */ private static final float LOAD_FACTOR = 0.95f; /** * Removes the duplicates by modifying the source list and keeping the order of the elements. * * @param <T> * the generic type * @param list * the list * @return the same object as the argument */ public static <T> List<T> removeDuplicates(List<T> list) { Set<T> temp = createLinkedHashSet(list.size()); temp.addAll(list); list.clear(); list.addAll(temp); return list; } /** * Creates the linked hash set instance that will hold the given amount of elements. The returned instance is * optimized for that size and will not grow until the given number of elements are added. * <p> * The method is same as <code>new LinkedHashSet<V>((int) (size * 1.1), 0.95f)</code> * * @param <V> * the value type * @param size * the preferred size * @return the map instance */ public static <V> Set<V> createLinkedHashSet(int size) { return new LinkedHashSet<>((int) (size * SIZE_NORMALIZATION), LOAD_FACTOR); } }