Today's Question:  What are you most afraid of as a programmer?        GIVE A SHOUT

Technical Article => Programming =>  Java

3 ways to remove duplicates in List

  sonic0002      2012-09-03 09:44:32      5,348    0    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 CLEAR LIST DUPLICATE

  SAVE AS PDF   MARK AS READ   MARK AS IMPORTANT

Share on Facebook  Share on Twitter  Share on Google+  Share on Weibo  Share on Reddit  Share on Digg  Share on Tumblr    Delicious

  RELATED


  0 COMMENT


No comment for this article.


  WRITE ARTICLE

I am looking for an IT support job

By sonic0002
When an IT support engineer goes to a new job interview, how does s/he describes her/his job experience? "Because there are no issues encountered after I deployed the application, so later I was fired and I am looking for a new job."