Open In App

Converting LinkedHashSet to Different Collection Types in Java

In Java, LinkedHashSet is a Class in the Java Collections Framework that extends the Collection HashSet and maintains the insertion order of the elements. The underlined data structure of the LinkedHastSet is that the insertion order is preserved.

Converting LinkedHashSet to Other Collections

In certain scenarios, we might need to convert the LinkedHashSet to any other collections like List or Map based on the requirement. This is a common requirement while dealing with diverse data structures or when we need to interface with APIs or libraries that expect different types of collections.



List Conversion

1. Using Constructor: By utilizing the constructor of the desired List implementation (like ArrayList) and passing the LinkedHashSet as an argument.

List<String> list = new ArrayList<>(linkedHashSet);

Example of the above Program:




// Java Program to Convert LinkedHashSet to List
import java.util.*;
  
// Driver Class
public class LinkedHashSetToList {
      // Main function
    public static void main(String[] args)
    {
        // Create a LinkedHashSet
        LinkedHashSet<String> linkedHashSet = new LinkedHashSet<>();
        linkedHashSet.add("Apple");
        linkedHashSet.add("Banana");
        linkedHashSet.add("Orange");
  
        // Convert LinkedHashSet to List using constructor
        List<String> list = new ArrayList<>(linkedHashSet);
  
        // Print the LinkedHashSet and List
        System.out.println("LinkedHashSet: " + linkedHashSet);
        System.out.println("List: " + list);
    }
}

Output:



Explaination of the above Program:

In the above program A LinkedHashSet is created with some elements And then the ArrayList Constructor is used to convert the LinkedHashSet to List. And here is the output for the above program

2. Using Iterator

Below is the implementation of the above Program:




// Java Program to Convert LinkedHashSet to List Using Iterator
import java.util.*;
  
// Driver Class
public class LinkedHashSetToListUsingIterator {
      // Main Function
    public static void main(String[] args) {
        // Create a LinkedHashSet
        LinkedHashSet<String> linkedHashSet = new LinkedHashSet<>();
        linkedHashSet.add("Java");
        linkedHashSet.add("python");
        linkedHashSet.add("c#");
  
        // Convert LinkedHashSet to List using iterator
        List<String> list = new ArrayList<>();
        Iterator<String> iterator = linkedHashSet.iterator();
        while (iterator.hasNext()) {
            list.add(iterator.next());
        }
  
        // Print the LinkedHashSet and List
        System.out.println("LinkedHashSet: " + linkedHashSet);
        System.out.println("List: " + list);
    }
}

Output

Explaination of the above Program:

In the above program A LinkedHashSet is created with some fields and an empty Arraylist is created , And then an ‘Iterator’ is used to iterate over the LinkedHashSet and each element to the ArrayList.And here is the output of the above Program

Map Conversion

1. Using Constant Values :




// Java Program to Convert LinkedHashSet to Map With Constant Values
import java.util.*;
  
// Driver Class
public class LinkedHashSetToMapWithConstantValues {
      // Main Function
    public static void main(String[] args) {
        // Create a LinkedHashSet
        LinkedHashSet<String> linkedHashSet = new LinkedHashSet<>();
        linkedHashSet.add("Apple");
        linkedHashSet.add("Banana");
        linkedHashSet.add("Orange");
  
        // Convert LinkedHashSet to Map with constant values
        Map<String, Integer> mapWithConstantValues = new HashMap<>();
        linkedHashSet.forEach(item -> mapWithConstantValues.put(item, 1));
  
        // Print the LinkedHashSet and Map with Constant Values
        System.out.println("LinkedHashSet: " + linkedHashSet);
        System.out.println("Map with Constant Values: " + mapWithConstantValues);
    }
}

Output :

Explaination of the above Program:

In the above program a LinkedHashset is created with some values and a HashMap is created, And forEach method is used to iterate over the LinkedHashSet and each element with a constant value (1) to the HashMap.

2. Using Derived Values




// Java Program to Convert LinkedHashSet to Map With Derived Values
import java.util.*;
  
// Driver Class
public class LinkedHashSetToMapWithDerivedValues {
      // Main Function
    public static void main(String[] args) {
        // Create a LinkedHashSet
        LinkedHashSet<String> linkedHashSet = new LinkedHashSet<>();
        linkedHashSet.add("Apple");
        linkedHashSet.add("Banana");
        linkedHashSet.add("Orange");
  
        // Convert LinkedHashSet to Map with values derived from keys
        Map<String, Integer> mapWithDerivedValues = new HashMap<>();
        linkedHashSet.forEach(item -> mapWithDerivedValues.put(item, item.length()));
  
        // Print the LinkedHashSet and Map with Derived Values
        System.out.println("LinkedHashSet: " + linkedHashSet);
        System.out.println("Map with Derived Values: " + mapWithDerivedValues);
    }
}

Output :

Explaination of the above Program:

In the above program a LinkedHashset is created with some values and a HashMap is created, And forEach method is used to iterate over the LinkedHashSet and each element with with a value derived from its length to HashMap.


Article Tags :