Java Initialize Set with Values in One Line
- Details
- Written by Nam Ha Minh
- Last Updated on 18 July 2024   |   Print Email
In this Java Collections post, I’d like to share with you some tips about how to create a Set object that is initialized with some fixed values in just a single line. The code examples are also provided.
You know, Set is a type of collection that store elements in unspecified order. Normally one would create a Set and add some elements like this:
Set<Integer> numbers = new HashSet<>(); numbers.add(123); numbers.add(456); numbers.add(789); numbers.add(910);
However, this way is quite boilerplate if the number of elements need to be added growing, e.g. 20 elements. There should be a shorter and better way, right?
Let’s look at two different ways you can use to initialize a Set object with some fixed values in the Java Collections framework.
1. Using Set.of() method
Instead of creating a Set object and adding each elements line by line, use the static method of() in the Set interface as below:
Set<Integer> numbers = Set.of(123, 456, 789, 910);
You see, using this way you can create a Set and initialize as many elements as you want in just a single line. Very convenient, right?
Similarly, the following code example initializes a Set of String elements in just one line:
Set<String> cards = Set.of("Ace", "Jack", "Queen", "King");
NOTES:
The Set.of() method returns an immutable collection, which means you can’t add or remove elements after initialization. Any attempts to do so will throw UnsupportedOperationException at runtime.
So if you want to create a mutable Set whose elements can be added/removed later, you need to create a new HashSet object that wraps the immutable one, as shown below:
Set<Integer> numbers = new HashSet<>(Set.of(123, 456, 789, 910));
This adds all elements from the Set collection returned by Set.of() method into a new HashSet object, thus you can modify the set later.
2. Using Streams API
Alternatively, you can use Java Streams API to create and initialize a Set object with some fixed elements in just a single line. The following line of code gives you the idea:
Set<Integer> numbers = Stream.of(123, 456, 789, 910).collect(Collectors.toSet());
Note that the returned Set collection is mutable so you can add or remove elements afterward if needed. Similarly, the following example shows the initialization of a Set that contain String elements using Java Streams API:
Set<String> cards = Stream.of("Jack", "Queen", "King", "Ace").collect(Collectors.toSet());
Summary:
In this article, you’ve learned two neat ways which you can use to create a Set collection that is loaded with some initial elements: using List.of() and Stream.of() methods. The former returns an immutable Set object whereas the latter creates a mutable one. And you may need to create a new HashSet object explicitly to get a modifiable Set. I recommend you watch the following video to see the coding of Set initialization in action:
References:
Other Java Collections Tutorials:
- What is Java Collections Framework?
- Java Queue Tutorial
- Java List Tutorial
- Java Set Tutorial
- Java Map Tutorial
- Understand equals and hashCode
- Understand object ordering
- 18 Java Collections and Generics Best Practices
Comments