Remove duplicate elements from an ArrayList in Java

0 votes
How can I eliminate duplicate elements from Java ArrayList
Apr 30, 2018 in Java by developer_1
• 3,310 points
688 views

2 answers to this question.

0 votes

You can perform this task by performing the following:

arrayList = new ArrayList<String>(new LinkedHashSet<String>(arrayList));
answered May 1, 2018 by sharth
• 3,350 points
0 votes
List<String> al = new ArrayList<>();
// add elements to al, including duplicates
Set<String> hs = new HashSet<>();
hs.addAll(al);
al.clear();
al.addAll(hs);
answered Jul 26, 2018 by Sushmita
• 6,900 points

Related Questions In Java

0 votes
1 answer

Remove duplicates from an ArrayList in Java

Here is the function for doing this. // ...READ MORE

answered Mar 6, 2019 in Java by Heena
3,093 views
0 votes
2 answers

How can we remove an element from an array in Java?

You can use ArrayUtils class remove method which ...READ MORE

answered May 24, 2018 in Java by UshaK
1,382 views
0 votes
2 answers

One line initialization of an ArrayList object in Java

In Java 8 or earlier: List<String> string = ...READ MORE

answered Jul 26, 2018 in Java by samarth295
• 2,220 points
2,194 views
0 votes
4 answers

Remove extra spaces from string in java

import java.util.regex.Matcher; import java.util.regex.Pattern; String pattern="[\\s]"; String replace=""; part="name=john age=13 year=2001"; Pattern ...READ MORE

answered Sep 10, 2018 in Java by Parth
• 4,630 points
1,445 views
0 votes
1 answer

Remove objects from an array in Java?

We can use external libraries: org.apache.commons.lang.ArrayUtils.remove(java.lang.Object[] array, int ...READ MORE

answered Jun 26, 2018 in Java by scarlett
• 1,290 points
378 views
0 votes
1 answer

Sorting an ArrayList in Java

You can easily do this by simply ...READ MORE

answered May 5, 2018 in Java by geek.erkami
• 2,680 points
840 views
0 votes
1 answer

How do I join 2 lists

This is not a simpler method, but ...READ MORE

answered May 11, 2018 in Java by sharth
• 3,350 points
127 views
0 votes
1 answer

Convert comma-separated String to ArrayList?

List<String> alist = Arrays.asList(str.split("\\s*,\\s*")); The above code splits ...READ MORE

answered May 17, 2018 in Java by sharth
• 3,350 points
1,639 views
0 votes
1 answer

How can I Sort an ArrayList in Java

You can sort the ArrayList in 2 ...READ MORE

answered Apr 23, 2018 in Java by sharth
• 3,350 points
318 views
0 votes
2 answers

When to use LinkedList and ArrayList in Java?

ArrayList is what you want. LinkedList is almost always a ...READ MORE

answered Dec 11, 2018 in Java by Sushmita
• 6,900 points
383 views