Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/**
 * Aptana Studio
 * Copyright (c) 2005-2011 by Appcelerator, Inc. All Rights Reserved.
 * Licensed under the terms of the GNU Public License (GPL) v3 (with exceptions).
 * Please see the license.html included with this distribution for details.
 * Any modifications to this file must keep this entire header intact.
 */

import java.util.ArrayList;

import java.util.LinkedHashSet;
import java.util.List;

import java.util.Set;

public class Main {
    /**
     * Given a list of elements of type <T>, remove the duplicates from the list in place
     * 
     * @param <T>
     * @param list
     */
    public static <T> void removeDuplicates(List<T> list) {
        // uses LinkedHashSet to keep the order
        Set<T> set = new LinkedHashSet<T>(list);

        list.clear();
        list.addAll(set);
        if (list instanceof ArrayList) {
            ((ArrayList<T>) list).trimToSize();
        }
    }
}