Communiquez avec les autres et partagez vos connaissances professionnelles

Inscrivez-vous ou connectez-vous pour rejoindre votre communauté professionnelle.

Suivre

How to remove duplicates elements from ArrayList in Java?

user-image
Question ajoutée par Mohammad Abrar Abrar , Team Leader , Enkindle Technologies Pvt Ltd
Date de publication: 2016/06/13

//By using Set

int last=arr.length;

Set<Integer> myv=newHashSet<Integer>();for(int i =0; i < last; i++){myv.add(arr[i]);}//display resultIterator it = myv.iterator();while(it.hasNext()){System.out.println(it.next());}

Utilisateur supprimé
par Utilisateur supprimé

As mentioned above, there are many ways.

1. Copying all the elements of ArrayList to LinkedHashSet.

2. Emptying the ArrayList, you can use clear() method to remove all elements of ArrayList and start fresh. 

 

3. Copying all the elements of LinkedHashSet (non-duplicate elements) to the ArrayList. 

durga yamini komera
par durga yamini komera , Associate software engineer , vijay infoart solutions

If you don't want duplicates in a  collection, you should consider why you're using a collection   that allows duplicates. The easiest way to remove repeated elements is to add the contents to a set (which will not allow duplicates) and then add the set back to the arrayList

RAJA BABU JHA
par RAJA BABU JHA , STUDENT , SOFCON INDIA TRAINING PVT LTD

USE THE FOR LOOP

ELSE USING ARRAHY CLASS IN COLLECTION

 

he easiest way to remove repeated elements is to add the contents to a Set (which will not allow duplicates) and then add the Set back to the ArrayList : List<String> al = new ArrayList<>(); // add elements to al, including duplicates Set<String> hs = new HashSet<>(); hs.addAll(al); al.clear(); al.addAll(hs);

LAGHRISSI Othmane
par LAGHRISSI Othmane , Consultant JAVA/JEE , Boursorama

En utilisant un Set (qui n'accepte pas les doublons)

Mahesh Dapkar
par Mahesh Dapkar , Shekhar Enterprises

we can use Set for that add ArrayList to HashSet or LikedHashSet then automatically Set remove all duplicate element. or we have another way for that we conver ArrayList to Array and check each and every element using index

Thaer Saidi
par Thaer Saidi , It Systems Design Engineer , Tata Consultancy Services

The easiest way to remove repeated elements is to add the contents to a Set 

List<String> al = new ArrayList<>();

including duplicateSet <String> hs = new HashSet<>();

hs.addAll(al);

al.clear();

al.addAll(hs);

If you don't want duplicates in a Collection, you should consider why you're using a Collectionthat allows duplicates. The easiest way to remove repeated elements is to add the contents to a Set(which will not allow duplicates) and then add the Set back to the ArrayList:

List<String> al =newArrayList<>();// add elements to al, including duplicatesSet<String> hs =newHashSet<>(); hs.addAll(al); al.clear(); al.addAll(hs);

SIVASHANKAR CHANDRASEKARAN
par SIVASHANKAR CHANDRASEKARAN , Software Engineer , Colan Infotech Private Limited

There are many way to do this... 

1. With using HashSet:

 HashSet: It remove duplicates from the list.

 

ArrayList<String> arrList = new ArrayList<String>();

// add elements to "arrList", including duplicates

ArrayList<String> remDuplicateList= new ArrayList<String>(new HashSet<String>(arrList));

 

2. Only using ArrayList:

ArrayList<String> secondList = new ArrayList<String>();

Iterator iterator = arrList.iterator();

        while (iterator.hasNext()) {

        String o = (String) iterator.next();

            if(!secondList.contains(o)) 

            secondList.add(o);

        }

static ArrayList<String> removeDuplicates(ArrayList<String> list)

which can be used to delete the duplicate elements from arraylist in Java.

More Questions Like This