Remove duplicates from an ArrayList?

耗尽温柔 提交于 2019-12-20 02:13:10

问题


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>
}

回答1:


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



回答2:


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




回答3:


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);
                        }
                    }
            }
        }
    */

}



回答4:


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.



来源:https://stackoverflow.com/questions/17627358/remove-duplicates-from-an-arraylist

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!