3 ways to remove duplicates in List

  sonic0002        2012-09-03 09:44:32       9,168        0    

Frequently, we may have an ArrayList which stores many values, and we need to process the ArrayList and get what are the distinct values in the list, or we may want to count occurrence of each value in the ArrayList. We can remove the duplicates in a few ways. Here we propose 3 methods :

    public static void main(String[] args){
        //SuperClass sub=new SubClass();
        
        String[] ar = { "dd", "c", "dd", "ff", "b", "e", "e" };
        ArrayList list = new ArrayList();
        for (int i = 0; i < ar.length; i++) {
            list.add(ar[i]);
        }
        System.out.println("Before running : ");
        for (int i = 0; i < list.size(); i++) {
            System.out.print(list.get(i) + " ");
        }
        System.out.println();
        /* First method */
        /**
         * A set will only store non-duplicate value
         */
        HashSet h = new HashSet(list);
        list.clear();
        list.addAll(h);
        /* First method */

        /* Second method */
        /**
         * Second method also uses the set, if the set has the value existed already,
         * the add method will return false, so the value will not be added to
         * the newList
         */
        Set set = new HashSet();
        List newList = new ArrayList();
        for (Iterator iter = list.iterator(); iter.hasNext();) {
            Object element = iter.next();
            if (set.add(element))
                newList.add(element);
        }
        list.clear();
        list.addAll(newList);
        /* Second method */

        /* Third method */
        /**
         * Normal way, remove the duplicate value from the list using two for
         * loops to check on the same list
         */
        for (int i = 0; i < list.size() - 1; i++) {
            for (int j = list.size() - 1; j > i; j--) {
                if (list.get(j).equals(list.get(i))) {
                    list.remove(j);
                }
            }
        }
        /* Third method */

        System.out.println("After running:");
        for (int i = 0; i < list.size(); i++) {
            System.out.print(list.get(i) + " ");
        }
    }

There should be other simple, efficient ways achieving this. We will add them.

Reference : http://www.javamm.com/?p=8149

JAVA  LIST  DUPLICATE  CLEAR 

       

  RELATED


  0 COMMENT


No comment for this article.



  RANDOM FUN

How to write proper resume