This example shows how to convert an array to LinkedHashSet in Java. This example also shows how to convert array to LinkedHashSet using constructor, addAll method, Java 8 Stream, and Apache Commons library.
How to convert an array to LinkedHashSet in Java?
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 before we can use that, we need to convert array to a List using the asList
method of the Arrays class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import java.util.Arrays; import java.util.LinkedHashSet; import java.util.Set; public class ArrayToLinkedHashSetExample { public static void main(String[] args) { String[] strColors = {"blue", "green", "red", "white", "black"}; /* * First convert an array to List and then * use the LinkedHashSet constructor */ Set<String> lhSetColors = new LinkedHashSet<String>( Arrays.asList(strColors) ); System.out.println("LinkedHashSet contains: " + lhSetColors); } } |
Output
1 |
LinkedHashSet contains: [blue, green, red, white, black] |
2. Using the asList method and addAll method
Similar to the above given approach, we will first convert an array to a List and then add all elements of the List to the linked hash set object using the addAll
method.
1 2 3 4 5 6 7 8 9 |
String[] strColors = {"blue", "green", "red", "white", "black"}; //create new empty LinkedHashSet object Set<String> lhSetColors = new LinkedHashSet<String>(); //add all elements of list to LinkedHashSet lhSetColors.addAll( Arrays.asList(strColors) ); System.out.println("LinkedHashSet contains: " + lhSetColors); |
Output
1 |
LinkedHashSet contains: [blue, green, red, white, black] |
3. Using the addAll method of the Collections class
Instead of using the addAll
method of the LinkedHashSet class, we can also use the addAll
method of the Collections class as given below.
1 2 3 4 5 6 7 8 9 |
String[] strColors = {"blue", "green", "red", "white", "black"}; //create new empty LinkedHashSet object Set<String> lhSetColors = new LinkedHashSet<String>(); //add all elements of list to LinkedHashSet Collections.addAll(lhSetColors, strColors); System.out.println("LinkedHashSet contains: " + lhSetColors); |
We do not need an additional step of converting the array to a List using this approach, as the addAll
method directly accepts an array.
4. Using Apache Commons Library
If you are using the Apache Commons library in your project, you can use the addAll
method of the CollectionUtils class as given below.
1 2 3 4 5 6 7 8 9 |
String[] strColors = {"blue", "green", "red", "white", "black"}; //create new empty LinkedHashSet object Set<String> lhSetColors = new LinkedHashSet<String>(); //use the addAll method of the CollectionUtils class CollectionUtils.addAll(lhSetColors, strColors); System.out.println("LinkedHashSet contains: " + lhSetColors); |
Output
1 |
LinkedHashSet contains: [blue, green, red, white, black] |
5. Using a loop
We can iterate through elements of the array using a for loop or enhanced for loop and add elements to LinkedHashSet object one by one as given below.
1 2 3 4 5 6 7 8 9 10 11 |
String[] strColors = {"blue", "green", "red", "white", "black"}; //create new empty LinkedHashSet object Set<String> lhSetColors = new LinkedHashSet<String>(); //iterate an array and add elements one by one for(String strColor : strColors){ lhSetColors.add(strColor); } System.out.println("LinkedHashSet contains: " + lhSetColors); |
Output
1 |
LinkedHashSet contains: [blue, green, red, white, black] |
6. Using Java 8 stream
If you are using Java version 8 or later, you can also use the stream to convert an array to a Set as given below.
1 2 3 4 5 |
String[] strColors = {"blue", "green", "red", "white", "black"}; Set<String> setColors = Arrays.stream(strColors).collect(Collectors.toSet()); System.out.println("Set contains: " + setColors); |
Output
1 |
Set contains: [red, green, blue, white, black] |
This example is a part of the LinkedHashSet in Java Tutorial with Examples.
Please let me know your views in the comments section below.
References:
Java 8 LinkedHashSet