ابدأ بالتواصل مع الأشخاص وتبادل معارفك المهنية

أنشئ حسابًا أو سجّل الدخول للانضمام إلى مجتمعك المهني.

متابعة

How to remove duplicates elements from ArrayList in Java?

user-image
تم إضافة السؤال من قبل Mohammad Abrar Abrar , Team Leader , Enkindle Technologies Pvt Ltd
تاريخ النشر: 2016/06/13
Mohamed Ghzayel
من قبل Mohamed Ghzayel

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

مستخدم محذوف‎
من قبل مستخدم محذوف‎

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
من قبل 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
من قبل RAJA BABU JHA , STUDENT , SOFCON INDIA TRAINING PVT LTD

USE THE FOR LOOP

ELSE USING ARRAHY CLASS IN COLLECTION

 

Pavan Patil
من قبل Pavan Patil

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
من قبل LAGHRISSI Othmane , Consultant JAVA/JEE , Boursorama

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

Mahesh Dapkar
من قبل 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
من قبل 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);

Sayan Ray
من قبل Sayan Ray

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
من قبل 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);

        }

Vignesh Kumar S
من قبل Vignesh Kumar S

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

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

المزيد من الأسئلة المماثلة