Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (4k points)
I have an ArrayList<String>, and I want to remove repeated strings from it. How can I do this?

1 Answer

0 votes
by (46k points)

 Don't consider  Collection that allows duplicates. The easiest method to eliminate duplicated elements is to attach the contents to a Set (which will not allow duplicates) and then add the Set back to the ArrayList:

Set<String> set = new HashSet<>(yourList);

yourList.clear();

yourList.addAll(set);

this will end the ordering of the elements in the ArrayList. 

Hope this Helps. Cheers..!!

Browse Categories

...