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 | { 8 Comments }

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

 

What you are thinkig....

8 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…..

If you want a pic to show with your comment, go get a gravatar !
Please post your questions on Java4s Answers forum

Name*
Ask a Question ?
or
Mail*
Website



By posting your answer, you agree to our comments policy.
Most Recent Tutorials
Hibernate Recent Posts
Spring Recent Posts
Struts Recent Posts
Recomandded Links Current & UpComing Tutorials Java4s.com
Tutorials Online :
spring Hibernate struts Json Ajax Log4j Log4j
coreJava Servlets


UpComing :
Jsps,Web Services
Java4s is optimized for learning java technologies, all the examples in this site are constantly reviewed to avoid errors. While using this site you agree to have read and accepted our terms of use and privacy policy
Especially i have prepared this blog by keeping fresher's in mind, however it will be very useful for real time developers too.


© 2014 Java4s All rights reserved. | strPro4Tut v(2.0) Theme designed by Team Java4s.