EN
Java - convert array to LinkedHashSet
4 points
In this short article we are going to show how to convert array to LinkedHashSet
in Java.
Following steps are necessary:
- convert array to list with
Arrays.asArray()
function, - create
LinkedHashSet
with constructor from the list.
Quick solution:
xxxxxxxxxx
1
new LinkedHashSet<String>(Arrays.asList(array));
Practical example:
xxxxxxxxxx
1
import java.util.Set;
2
import java.util.Arrays;
3
import java.util.LinkedHashSet;
4
5
public class ArrayToLinkedHashSetExample {
6
7
public static void main(String[] args) {
8
9
String[] array = {"blue", "green", "red"};
10
Set<String> set = new LinkedHashSet<>(Arrays.asList(array));
11
12
System.out.println("LinkedHashSet: " + set);
13
}
14
}
Output:
xxxxxxxxxx
1
LinkedHashSet: [blue, green, red]