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 .::

Java4s_Author
Sivateja Kandula - Java/J2EE Full Stack Developer
Founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog, Love Java and UI frameworks.
You can sign-up for the Email Newsletter for your daily dose of Java tutorials.

Comments

22 Responses to “How to Remove Duplicate Values From Java List/ArrayList?”
  1. prasad says:

    Hi , If you have any interview questions are their please forward me and this mail Id:prasada0035@gmail.com,

    Thanks

  2. reddy says:

    I do not want to pass the list to set there is any other way to remove duplicates

  3. Sunidhar says:

    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);
    }
    }

  4. 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);

    }
    }
    }

  5. Amit Kumar says:

    I want to read How to work == and equals method

  6. Nitya says:

    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

  7. suresh says:

    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

  8. how can i provide security to my java project?

    please help me…..

  9. Deepak N S says:

    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());
    }
    }
    }

  10. laltu says:

    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

  11. ashis says:

    sir, please let me know how to remove duplicate from arraylist by overriding equal() and hashcode()?

  12. Raj Kumar says:

    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

  13. Venkat says:

    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

  14. Ravi says:

    Nice Explanation..

  15. Abneet Rana says:

    Thanks.. Nice logic. This is easy and fast processing.

  16. Shaik Hasan Saheb says:

    How to custamize hashing mechanisam in hashmap

  17. yogesh says:

    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);

  18. Mukund says:

    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);
    }
    }

    }

  19. sindhuja Alladi says:

    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 ?

Name*
Mail*
Website



By posting your answer, you agree to our comments policy.
Most Recent Posts from Top Categories
Spring Boot Hibernate Spring
Contact | About Us | Privacy Policy | Advertise With Us

© 2010 - 2024 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.