Java – delete duplicates from ArrayList?
•
Java
How do I remove duplicates from ArrayList?
I have getccnptags array as [Java, PHP, C, Java, PHP]. I get the hyperlink I give to each array variable from the bean array, but I want to delete duplicates and then add hyperlinks. Can I add any code in my following code to delete 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> }
Solution
It is best to use HashSet If this is not possible, you can use a temporary HashSet
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
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码