Looking for java Answers? Try Ask4KnowledgeBase
Looking for java Keywords? Try Ask4Keywords

Java Language Declaring a HashSet with values


Example

You can create a new class that inherits from HashSet:

Set<String> h = new HashSet<String>() {{
    add("a");
    add("b");
}};

One line solution:

Set<String> h = new HashSet<String>(Arrays.asList("a", "b"));

Using guava:

Sets.newHashSet("a", "b", "c")

Using Streams:

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