Duplicate Elements/Strings can be removed in ArrayList many ways . One way will be using HashSet . Because HashSet does not allow duplicates. Another way can be using another ArrayList . Adding elements one by one from the first ArrayList to Second ArrayList by checking the element exists in the second array list .
//Example 1 : Using the Second Array List to remove duplicates
Output :
Before Removing Duplicate
1
2
3
3
1
2
3
4
After Removing Duplicate
1
2
3
4
//Example 2: Using the HashSet
Output :
Before Removing Duplicate
Sunday
Monday
Tuesday
Sunday
Wednesday
Tuesday
Thursday
Friday
Monday
Saturday
------------
After Removing Duplicate
Friday
Monday
Saturday
Sunday
Thursday
Tuesday
Wednesday
//Example 1 : Using the Second Array List to remove duplicates
import java.util.*;
public class ArrayListDupRemoval
{
public static void main (String[] args)
{
ArrayList arrayList1 = new ArrayList();
ArrayList arrayList2 = new ArrayList();
arrayList1.add("1");
arrayList1.add("2");
arrayList1.add("3");
arrayList1.add("3");
arrayList1.add("1");
arrayList1.add("2");
arrayList1.add("3");
arrayList1.add("4");
System.out.println("Before Removing Duplicate");
for (Object item : arrayList1)
System.out.println(item);
for (Object item : arrayList1)
{
if (!arrayList2.contains(item)) // checking the element exists in the second array list , if not then add to second array list.
{
arrayList2.add(item);
}
}
arrayList1.clear(); // Clears the first arraylist with duplicates
arrayList1=arrayList2; //
System.out.println("After Removing Duplicate");
for (Object item : arrayList1)
System.out.println(item);
}
}
Output :
Before Removing Duplicate
1
2
3
3
1
2
3
4
After Removing Duplicate
1
2
3
4
//Example 2: Using the HashSet
import java.util.*;
public class ArrayListDupRemovalHashSet
{
public static void main (String[] args)
{
ArrayList arrayList1 = new ArrayList();
arrayList1.add("Sunday");
arrayList1.add("Monday");
arrayList1.add("Tuesday");
arrayList1.add("Sunday");
arrayList1.add("Wednesday");
arrayList1.add("Tuesday");
arrayList1.add("Thursday");
arrayList1.add("Friday");
arrayList1.add("Monday");
arrayList1.add("Saturday");
System.out.println("Before Removing Duplicate");
for (Object item : arrayList1)
System.out.println(item);
System.out.println("------------");
HashSet hashSet = new HashSet(arrayList1); //Creating HashSet which does not allows duplicates
arrayList1.clear(); // Clearing arraylist1
arrayList1 = new ArrayList(hashSet) ; // Assigning Hashset back with no duplicates to arraylist1
//Note : Hashset does not any maintain order
System.out.println("After Removing Duplicate");
for (Object item : arrayList1)
System.out.println(item);
}
}
Output :
Before Removing Duplicate
Sunday
Monday
Tuesday
Sunday
Wednesday
Tuesday
Thursday
Friday
Monday
Saturday
------------
After Removing Duplicate
Friday
Monday
Saturday
Sunday
Thursday
Tuesday
Wednesday
No comments:
Post a Comment
I'm certainly not an expert, but I'll try my hardest to explain what I do know and research what I don't know.