How can I initialize set in Java with initial values in a single line?

For flexibility, you can use Java Streams to collect values into a set.

Set<String> h = Stream.of("a", "b", "c").collect(Collectors.toSet());

:white_check_mark: Why This Works:

Uses Stream.of() to create a stream of elements and then collects them into a set.

Allows transformation of data before collecting.

:x: Downside:

Slightly less readable for simple cases.

Less efficient for small sets due to stream overhead.

Usage Example:

Set<Double> uniqueSquares = Stream.of(2.0, 3.0, 4.0)
                                  .map(x -> x * x)
                                  .collect(Collectors.toSet());