Newsletter |
How to Remove Duplicate Values From Java List/ArrayList?
Core Java » on Oct 13, 2012 { 22 Comments } By Sivateja
This is the question asked in our forum by some of our friend, even i faced this little issue once in my project 🙂 Some times we may not have chance to choose our own collection property. For example in Hibernate HQL we used to get the output through List object, what if you get some duplicate records in our List ? not only in Hibernate, you might saw in regular usage of List as well.
So we will see how to remove duplicate objects from List or ArrayList collection.
RemDupFromList.java
package java4s; import java.util.ArrayList; import java.util.HashSet; import java.util.Iterator; import java.util.List; public class RemDupFromList { public static void main(String[] args) { List li = new ArrayList(); li.add("one"); li.add("two"); li.add("three"); li.add("one");//Duplicate li.add("one");//Duplicate // We have facility to pass a List into Set constructor and vice verse to cast List li2 = new ArrayList(new HashSet(li)); //no order // List li2 = new ArrayList(new LinkedHashSet(li)); //If you need to preserve the order use 'LinkedHashSet' Iterator it= li2.iterator(); while(it.hasNext()) { System.out.println(it.next()); } } }
Explanation
- Take your normal List object
- Pass that List li object to Set [Line number 22]Â => So finally we have Set object in our hand, just pass this current Set object as argument to ArrayList, so we got new List object li2 without duplicate
- But if you would like to preserve the order of data use LinkedHashSet rather HashSet
Output
one
two
three
Thing is……
Set setObject = new HashSet(listObject);
List listObject = new ArrayList(setObject);
Hope you got it 🙂
​ ​​
You Might Also Like
::. About the Author .:: | ||
Comments
22 Responses to “How to Remove Duplicate Values From Java List/ArrayList?”
Hi , If you have any interview questions are their please forward me and this mail Id:prasada0035@gmail.com,
Thanks
Hi Prasad,
How could we can retrieve distinct values from list of objects of some column value
By using hibernate projection you could achieve this purpose.
I do not want to pass the list to set there is any other way to remove duplicates
yes,, follow this code
import java.util.ArrayList;
import java.util.Iterator;
class Duplicate
{
public static void main(String[] args)
{
System.out.println(“Hello World!”);
ArrayList a1 = new ArrayList();
ArrayList a2 = new ArrayList();
ArrayList a3 = new ArrayList();
a1.add(10);
a1.add(20);
a1.add(30);
a1.add(40);
a1.add(50);
a1.add(10);
a1.add(40);
a1.add(60);
a1.add(70);
a1.add(50);
a1.add(80);
a1.add(90);
a1.add(20);
a1.add(30);
System.out.println(a1);
Iterator i1 = a1.iterator();
a1.removeDuplicate
while(i1.hasNext())
{
int i = (Integer)i1.next();
if(a2.contains(i))
{
a3.add(i);
i1.remove();
}
else
{
a2.add(i);
}
}
System.out.println(“”);
System.out.println(“Elements without duplicate “);
System.out.println(a2);
System.out.println(“”);
System.out.println(“Duplicate elements are”);
System.out.println(a3);
}
}
i dont think, it remove the duplicated elements, instead it removed the element itself
Print Duplicate elements in arraylist This is Value Labs interview question:
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
public class CountDuplicatedList {
public static void main(String[] args) {
List list = new ArrayList();
list.add(“a”);
list.add(“b”);
list.add(“c”);
list.add(“d”);
list.add(“b”);
list.add(“c”);
list.add(“a”);
list.add(“a”);
list.add(“a”);
Set uniqueSet = new HashSet(list);
for (String temp : uniqueSet) {
if(Collections.frequency(list, temp)>1)
System.out.println(“Duplicate elements:”+temp);
}
}
}
I want to read How to work == and equals method
What if i do not want my array list to contain duplicate object based on a particular property..What i mean is say for example “Employee” class has a property “empName” and i do not want my array list to contain object which has same “empName”..Thanks in advance
sir could you please provide the code to remove duplicates for user defined objects from the set that is if two different objects contains sane date how remove that duplicate data
how can i provide security to my java project?
please help me…..
RemoveDuplicate elements in arraylist without using hash set.
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
public class DuplicateList {
public static void main(String[] args) {
List li = new ArrayList();
li.add(“one”);
li.add(“two”);
li.add(“three”);
li.add(“one”);// Duplicate
li.add(“one”);// Duplicate
for(int i=0;i”+li.size());
Iterator it1=li.iterator();
while(it1.hasNext()){
System.out.println(it1.next());
}
}
}
hi Java4s, plz give tutorial of complete collection framework & other important topic of core java..if not then plz send me good link of website at laltukumarjha@gmail.com.
regards
laltu
sir, please let me know how to remove duplicate from arraylist by overriding equal() and hashcode()?
Hi ashis,
To remove duplicate from arraylist you can use set interface because set does not allow the duplicate values.please find below the example programe for your understanding
Duplicate Values From Java ArrayList using LinkedHashSet
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
public class RemoveDuplicate {
public static void main(String[] args) {
List sourceList = new ArrayList();
sourceList.add(“object1”);
sourceList.add(“object2”);
sourceList.add(“object2”);
sourceList.add(“object3”);
sourceList.add(“object4”);
sourceList.add(“object2”);
List newList = new ArrayList(new LinkedHashSet(
sourceList));
Iterator it = newList.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
}
}
Output
object1
object2
object3
object4
Hi,
I really appreciate you Mr.Sivateja. when i saw your examples not only corejava
and also spring etc. I can easily learn more from your posted examples
Nice Explanation..
Thanks.. Nice logic. This is easy and fast processing.
How to custamize hashing mechanisam in hashmap
List orignal=new ArrayList();
orignal.add(“yogesh”);
orignal.add(“hsegoy”);
orignal.add(“rathore”);
orignal.add(“erohtar”);
orignal.add(“yogesh”);
System.out.println(orignal);
Set duplicate=new HashSet();
duplicate.addAll(orignal);
System.out.println(duplicate);
Code to print duplicates given in comment is wrong
below is the right code
public class Demo {
public static void main(String[] args)
{
List li = new ArrayList();
li.add(“a”);
li.add(“a”);
li.add(“b”);
li.add(“a”);
li.add(“b”);
Set uniqueSet = new HashSet(li);
Iterator it = uniqueSet.iterator();
while(it.hasNext())
{
String temp =(String) it.next();
if(Collections.frequency(li,temp)>1)
System.out.println(temp);
}
}
}
this question asked in deloitte.. how will you remove duplicate records in arraylist if it consits of 2 lakh records ?
could you please let me know ?