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,300 points
131 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 Apr 30, 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,880 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 5 in Java by Heena
510 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
127 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,190 points
208 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 parth295
• 4,640 points
111 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,280 points
65 views
0 votes
1 answer

Sorting an ArrayList in Java

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

answered May 4, 2018 in Java by geek.erkami
• 2,640 points
225 views
0 votes
1 answer

How do I join 2 lists

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

answered May 10, 2018 in Java by sharth
• 3,350 points
31 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
424 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
75 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,880 points
134 views