Friday, September 5, 2025
HomeLanguagesJavaHow to Convert an Array to LinkedHashSet in Java?

How to Convert an Array to LinkedHashSet in Java?

An array is a collection of items stored at contiguous memory locations. An array can contain primitives data types as well as objects of a class depending on the definition of the array. Whereas LinkedHashSet contains only unique elements and they are stored in the same order in which they are inserted.

Examples:

Input: arr = {"A", "B", "C", "D", "A"}
Output: LinkedHashSet = {"A", "B", "C", "D"}
LinkedHashSet does not contain repeated elements.

Input: arr = {1, 3, 5, 3, 1}
Output: LinkedHashset = {1, 3, 5}

There are several ways using which we can convert an array to an object of the LinkedHashSet in Java as given below:

1.  Using the asList method and LinkedHashSet constructor

  • The LinkedHashSet class provides a constructor that accepts a collection object.
  • But to use that, we need to convert the array to a List using the asList method of the Arrays class.

Java




// Java program to convert an array to LinkedHashSet
  
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.Set;
  
public class ArrayToLinkedHashSet {
  
    public static void main(String[] args)
    {
  
        // array of string
        String[] names
            = { "John", "Devid", "Smith", "Joe", "Stark" };
  
        // First convert the name array to List and then
        // use the LinkedHashSet constructor to convert
        // array to linkedhasset
  
        Set<String> linkedhasset_name
            = new LinkedHashSet<String>(Arrays.asList(names));
  
        System.out.println(
            "LinkedHashSet contains element: " + linkedhasset_name);
    }
}


Output

LinkedHashSet contains element: [John, Devid, Smith, Joe, Stark]

2.  Using the asList method and addAll method

  • Similar to the previous approach, we will first convert an array to a List
  • But this time instead of using the constructor, we will use addAll method to add elements from the array to a linked hash set.

Java




// Java program to convert an array to LinkedHashSet
  
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.Set;
   
public class ArrayToLinkedHashSet {
   
    public static void main(String[] args) {
          
       String[] names = {"John", "Devid", "Smith", "Joe", "Stark"};
   
      //create new empty LinkedHashSet object to store array element    
      Set<String> linkhasset_name = new LinkedHashSet<String>();
  
      //add all elements of list to LinkedHashSet
      linkhasset_name.addAll( Arrays.asList(names) );
  
      System.out.println("LinkedHashSet contains: " + linkhasset_name);
    }
}


Output

LinkedHashSet contains: [John, Devid, Smith, Joe, Stark]

3.  Using Java 8 Stream API:

  • Similar to HashSet, you can also use toCollection() method of Collectors class to convert a Stream to LinkedHashSet.
  • First, convert array to stream then convert the stream to LinkedHashSet using collection stream which only works in java 8 version.
collect( Collectors.toCollection( LinkedHashSet::new ) );

Java




// Java program to convert an array to LinkedHashSet
  
import java.io.*; 
import java.util.ArrayList; 
import java.util.LinkedHashSet;
import java.util.*; 
import java.util.stream.*;
import java.util.Set;
   
public class ArrayToLinkedHashSet {
   
    public static void main(String[] args) {
          
    String[] names = {"John", "Devid", "Smith", "Joe", "Stark"};
   
    //create new empty LinkedHashSet object    
    Set<String> linkhasset_name = new LinkedHashSet<String>();
  
    //add all elements of list to LinkedHashSet
     linkhasset_name = Arrays.stream(names).collect( Collectors.toCollection( LinkedHashSet::new ) );
  
    System.out.println("LinkedHashSet contains: " + linkhasset_name);
  
    }
}


Output

LinkedHashSet contains: [John, Devid, Smith, Joe, Stark]

4.  Using a loop or brute force

  • We can iterate through all the elements of the array using a for loop and add elements to the LinkedHashSet object one by one as given below.

Java




// Java program to convert an array to LinkedHashSet
  
import java.io.*; 
import java.util.ArrayList; 
import java.util.LinkedHashSet;
import java.util.*; 
import java.util.Set;
   
public class ArrayToLinkedHashSet {
   
    public static void main(String[] args) {
  
      String[] names = {"John", "Devid", "Smith", "Joe", "Stark"};
  
      //create new empty LinkedHashSet object    
      Set<String> linkhasset_name = new LinkedHashSet<String>();
  
      //iterate an array and add elements one by one to the linkedhasset
      for(String name : names){
          linkhasset_name.add(name);
      }
  
      System.out.println("LinkedHashSet contains: " + linkhasset_name);
    }
}


Output

LinkedHashSet contains: [John, Devid, Smith, Joe, Stark]
RELATED ARTICLES

Most Popular

Dominic
32266 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6635 POSTS0 COMMENTS
Nicole Veronica
11801 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11864 POSTS0 COMMENTS
Shaida Kate Naidoo
6752 POSTS0 COMMENTS
Ted Musemwa
7026 POSTS0 COMMENTS
Thapelo Manthata
6703 POSTS0 COMMENTS
Umr Jansen
6720 POSTS0 COMMENTS