You can view the tutorials best in Google Chrome, Mozilla Firefox, Opera, higher version of Internet Explorer

How to Remove Duplicate Values From Java List/ArrayList?

Core Java » On Oct 13, 2012 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

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








By ( Java/J2EE Developer & Web Designer )
Founder of Java4s.com. You can contact me at sivateja [at] java4s.com


Comments

13 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. venkatnasam says:

    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

Name*
Mail*
Website



By posting your answer, you agree to our comments policy.
So, What would you like to learn today?
Browse these popular technologies
Hibernate spring struts Servlets ajax Log4j jQuery Json Core Java Web Services AngularJs

Most Recent Tutorials
Hibernate Recent Posts
Spring Recent Posts
Struts Recent Posts
Recomandded Links
Current & UpComing Tutorials
Sitemap :
spring Hibernate struts Json Ajax Log4j Log4j coreJava Servlets Web Services


Upcoming
  Web Services [ SOAP ], JSP
Facebook_Java4s  Twitter_Java4s  GooglePlus_Java4s  Youtube_Java4s 
© 2010 - 2014 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.