remove Duplicates from ArrayList of Long - Java Collection Framework

Java examples for Collection Framework:Array Auto Increment

Description

remove Duplicates from ArrayList of Long

Demo Code


//package com.java2s;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

public class Main {
    public static ArrayList<Long> removeDuplicates(ArrayList<Long> a_master) {
        ArrayList<Long> a = new ArrayList<Long>(a_master);
        Set<Long> set = new HashSet<Long>();
        List<Long> newList = new ArrayList<Long>();
        for (Iterator<Long> iter = a.iterator(); iter.hasNext();) {
            Long element = iter.next();
            if (set.add(element))
                newList.add(element);//  ww w . jav  a  2 s .com
        }
        a.clear();
        a.addAll(newList);
        return a;
    }
}

Related Tutorials