Declaring a HashSet with values
suggest changeYou 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());
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents