user2545106
user2545106

Reputation: 87

Remove duplicates from an ArrayList?

How to remove duplicates from an ArrayList?

I have getCcnptags array as [java,php,c++,c,java,php] which i am getting from bean array, and I am giving hyper link to each array variable, but I want to remove duplicates before adding hyper link to it, does it possible to add any code in my below code to remove duplicates.

for(int k=0;k<name.getCcnptags().size();k++)
    {

    String tag=name.getCcnptags().get(k);
        if(k!=name.getCcnptags().size()-1)
    {
    tag=tag+",";

    }
    %>
    <a href='#'><%=tag%></a>
}

Upvotes: 3

Views: 2844

Answers (4)

Jon C. Hammer
Jon C. Hammer

Reputation: 392

As a side note, if you need the items in your list to remain in their original order, the following approach an be used instead. The set is used to check for duplicate items, and another list is returned.

public list<Object> removeDuplicates(List<Object> list)
{
    List<Object> secondary = new LinkedList<Object>();
    Set<Object> auxillery  = new HashSet<Object>();

    for (Object o : list)
    {
        if (!auxillery.contains(o))
        {
            auxillery.add(o);
            secondary.add(o);
        }
    }

    return secondary;
}

Usage is like this (or similar):

public static final void main(String[] args)
{
    List<Integer> numbers = new LinkedList<Integer>();
    numbers.add(5); numbers.add(6); numbers.add(5); numbers.add(8);

    numbers = removeDuplicates(numbers);
}

Something to remember: if using custom classes, ensure that they override the standard Object.hashCode() method. Otherwise, this approach will not work properly.

Upvotes: 0

JavaTechnical
JavaTechnical

Reputation: 9357

I found this on the web. It is much similar to removing elements in a Vector but with change in method names.

import java.util.*;
class RemoveDuplicates
{

    public static void main(String args[])
    {
    ArrayList<String> v=new ArrayList<String>();
    v.add("Gowtham");
    v.add(" Gutha's");
    v.add(" Java");
    v.add("-");
    v.add("demos");
    v.add(".");
    v.add("blogspot");

    // '.' again!
    v.add(".");
    v.add("com ");

    // Gowtham again!
    v.add("gowtham");

    System.out.println("Original");

        for(int i=0;i<v.size();i++)
        {
            System.out.print(v.get(i));

        }

    System.out.println("\nAfter removing 

duplicates");
    removeDuplicates(v);

        for(int i=0;i<v.size();i++)
        {
            System.out.print(v.get(i));
        }

    }


    // Applicable for all types of ArrayLists

    public static void removeDuplicates(ArrayList 

v)
    {
        for(int i=0;i<v.size();i++)
        {
            for(int j=0;j<v.size();j++)
            {
                    if(i!=j)
                    {


if(v.get(i).equals(v.get(j)))
                        {
                        v.remove(j);
                        }
                    }
            }
        }
    }


    /*
        * Specifically applicable for String is 

written for equalIgnoreCase
        * The code..


        public static void 

removeDuplicates(ArrayList<String> v)
        {
            for(int i=0;i<v.size();i++)
            {
                for(int j=0;j<v.size();j++)
                {
                    if(i!=j)
                    {


if(v.get(i).equalsIgnoreCase(v.get(j)))
                        {
                        v.remove(j);
                        }
                    }
            }
        }
    */

}

Upvotes: 0

Suresh Atta
Suresh Atta

Reputation: 121998

Use Set interace,A collection that contains no duplicate elements.

From ArrayList create Hashset and use it.

Collection object has a constructor that accept a Collection object to initial the value.

ArrayList yourlist= new ArrayList();
HashSet nodupesSet= new HashSet(yourlist);

Now iterate over nodupesSet

Upvotes: 3

stinepike
stinepike

Reputation: 54682

Better use a HashSet. If not possible then you can use a temporary HashSet for this.

ArrayList a= new ArrayList();
HashSet hs = new HashSet();
hs.addAll(a);  // willl not add the duplicate values
a.clear();
a.addAll(hs);  // copy the unique values again to arraylist

Upvotes: 4

Related Questions