convert Array / ArrayList to Set in Java. Use LinkedHashSet to maintain Insertion-order

The following tutorial explains about how to convert Array / ArrayList to Set. What is Set and why we need Set?. A Set is a Collection with no duplicate elements. One of the main reason to convert List to Set is to remove duplicates in it. The following situations may arise for converting List (ArrayList) to Set

1. just to remove the duplicates in the ArrayList and no need to maintain the original ArrayList order. (convert ArrayList to HashSet)

2. to remove the duplicates in the ArrayList and preserve the ArrayList order (convert ArrayList to LinkedHashSet)

3. to remove the duplicates of the ArrayList and sorts the contents of the Set. (convert ArrayList to TreeSet)

This tutorial also covers that how to convert Array to Set. Now let us see some of the implementations of Set which are related to the above situations

1. HashSet – class that implements the Set interface, stores elements in hash table (through HashMap instance) and does not guarantee insertion order of the set.

2. LinkedHashSet – stores elements in a hash table with a linked list and maintains the insertion-order ( the order in which the elements were inserted into the set). Performance of LinkedHashSet is slightly lower than of HashSet, because it uses linked list. Iteration time is proportional to the size of the set, where as iteration time over a HashSet is proportional to its capacity.

3. TreeSet – implements the NavigableSet, elements are stored in Red-Black tree (through NavigableMap) and sorted based on their keys using their natural ordering. Slower than HashSet.

Now let us see the possible options of converting the List to Set

1. ArrayList to HashSet – eliminates the duplicate elements of ArrayList but does not guarantee to preserve the order of the ArrayList. When you convert ArrayList to HashSet, why the order of ArrayList is not maintained? I have answered this question in my earlier post Why HashSet / HashMap in java does not maintain the insertion order?

You can use any one of the following one line statement for the conversion

Set<Type> hashset = new HashSet<Type>(c); – just passing the ArrayList to the constructor of the HashSet

Collection<Type> hashset = new HashSet<Type>(c); – General form

Eg. List<String> colors = new ArrayList<String>();

Collections.addAll(colors, “Green” , “Red” , “Blue”, “Yellow” ); // initalize the arraylist

Set<String> hashset = new HashSet<String>(months);

OR

Collection<String> hashset = new HashSet<String>(months);

Complete code is as follows

import java.util.*;
public class ArrayListToSet1
{
public static void main( String[] args )
{
List<String> colors = new ArrayList<String>();
Collections.addAll(colors, "Green" , "Red" , "Blue", "Yellow" ); // initalize the arraylist
System.out.println("Before Removing Duplicates\n");
for (String color : colors){
System.out.println(color);
}
System.out.println();

Set<String> set = new HashSet<String>(colors);
//Collection<String> set = new HashSet<String>(colors);

System.out.println("After Removing Duplicates. But order of original arraylist is changed.....\n");
for (String color : set){
System.out.println(color);
}
System.out.println("\nOR\n");
System.out.println(set);
}
}

The above output shows , the HashSet does not maintain the order of the original collection ArrayList.

2. ArrayList to LinkedHashSet – eliminates the duplicate elements of ArrayList also preserves the order of the ArrayList .


Set<Type> linkedHashSet = new LinkedHashSet<Type>(c);
OR
Collection<Type> linkedHashSet = new LinkedHashSet<Type>(c);

Eg. Collections.addAll(months, “January” , “February” , “March” , “April” , “May”, “June”, “July”, “August”, “September”,”July”, “October”, “Novmber” , “December”); // initalize the arraylist


<b>Set<String> linkedHashSet = new LinkedHashSet<String>(months);
OR
Collection<Type> linkedHashSet = new LinkedHashSet<Type>(months);

Code :


........
............
System.out.println("Before Removing Duplicates\n");
System.out.println(months);
System.out.println();
Set<String> set = new LinkedHashSet<String>(months);
// Collection<String> set = new LinkedHashSet<String>(months);
System.out.println("After Removing Duplicates. But order of original arraylist is preserved.....\n");
System.out.println(set);
.............
..............

The above output shows , duplicates in the original ArrayList is removed and the order of list is maintained using LinkedHashSet.

2. ArrayList to TreeSet – eliminates the duplicate elements of ArrayList and the elements of the Set is sorted.

Syntax:


<b>Set<Type> treeSet = new TreeSet<Type>(c);
 OR
 Collection<Type> treeSet = new TreeSet<Type>(c);</b>

Eg. Collections.addAll(empCodes, 1000, 676, 4543, 9065, 1001);

Set<Integer> treeSet = new TreeSet<Integer>(empCodes);
 OR
 Collection<Integer> treeSet = new TreeSet<Integer>(empCodes);

Code Snippet


.............
.............
List<Integer< empCodes = new ArrayList<Integer<();
Collections.addAll(empCodes, 1000, 676, 4543, 9065, 1001);
System.out.println(empCodes);

//Set<Integer< treeSet = new TreeSet<Integer<(empCodes);
Collection<Integer< treeSet = new TreeSet<Integer<(empCodes);

System.out.println("Tree Set values in Ascending Order without duplicates.....");
 System.out.println(treeSet);
..............
....................

Array to Set:

Converting Array to Set is very simple .Just convert Array to ArrayList and do the above steps . To convert Array to ArrayList , use the Statement Arrays.asList(arr). Again one line statement will do .

Syntax:


Set<Type> arrToSet = new LinkedHashSet<Type>(Arrays.asList(arr));
 OR
 Collection<Type> arrToSet = new LinkedHashSet<Type>(Arrays.asList(arr));

Yo can also use HashSet or TreeSet instead of LinkedHashSet.

Eg. String[] wd={“Monday” , “Tuesday”, “Wednesday”, “Thursday”, “Friday”, “Saturday”, “Sunday”};


Set<String> wdSet = new LinkedHashSet<String>(Arrays.asList(wd));
 OR
 Collection<String> wdSet = new LinkedHashSet<String>(Arrays.asList(wd)); 

 

You may also like

Leave a Reply