Today's Question:  What's your opinion about Alibaba mooncake incident?        GIVE A SHOUT

Technical Article => Programming =>  Java

3 ways to remove duplicates in List

  sonic0002      2012-09-03 09:44:32      5,172    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

  RELATED


  0 COMMENT


No comment for this article.


  WRITE ARTICLE

What an embarrassment

By sonic0002
When someone wants to hire someone with 7-10 years NodeJS experience, the NodeJS author jumped out and said that he had just 7 years experience of NpdeJS because NodeJS was created in 2009, which is 7 years ago. What an embarrassment for the recruiter.