You can provide either Set.of or List.of factory method, since Java 9, or Arrays.asList factory method to the HashSet(Collection) constructor to create and init a HashSet in one line

Apart from that, you can use add and addAll methods after the creation time to initialize a HashSet

Let's walk through this tutorial to explore them in more details

Initialize in one line with Java 9+ Set.of and List.of

  • Provide either Set.of or List.of factory method, since Java 9+, to the HashSet(Collection) constructor to create and init a HashSet in one line
@Test
public void initWithListOfAndSetOf() {  
    // create and initialize a HashSet from Java 9+ List.of
    Set<Integer> set1 = new HashSet<>(List.of(3, 1, 2, 1, null));
    assertThat(set1).contains(3, 1, 2, null);

    // create and initialize a HashSet from Java 9+ Set.of
    Set<Integer> set2 = new HashSet<>(Set.of(5, 4, 6));
    assertThat(set2).contains(5, 4, 6);
}
  • List.of permits duplicated elements while Set.of doesn't, it throws IllegalArgumentException

    Both List.of and Set.of don't permit null element, they throw NullPointerException

Initialize in one line with Arrays.asList

  • Provide Arrays.asList to the HashSet(Collection) constructor to create and init a HashSet in one line

    Array.asList accepts duplicated and null elements

@Test
public void initWithArraysAsList() {  
    Set<Integer> set = new HashSet<>(Arrays.asList(3, 1, 2, 1, null));
    assertThat(set).contains(3, 1, 2, null);
}

Initialize in one line from an existing Collection

  • Provide an existing Collection to the HashSet(Collection) constructor to create and init a HashSet in one line
@Test
public void initFromAnExistingCollection() {  
    Set<Integer> set1 = new HashSet<>(Arrays.asList(3, 1, 2, null));

    Set<Integer> set2 = new HashSet<>(set1);
    assertThat(set2).contains(3, 1, 2, null);
}

Initialize by using add and addAll methods

  • You can use add or addAll method to initialize a HashSet after the creation time
@Test
public void initWithAddAndAddAll() {  
    Set<Integer> set1 = new HashSet<>(Set.of(1, 2, 3));

    Set<Integer> set2 = new HashSet<>();
    set2.add(1);
    set2.add(1); // duplicated element will be ignored
    set2.add(null); // permits null element
    set2.addAll(set1);

    assertThat(set2).contains(1, 2, 3, null);
}

Double brace initialization should not be used in practice

  • Double brace initialization can cause a memory leak as it creates anonymous classes with a hidden reference to the owner instance object
@Test
public void initWithDoubleBrace() {  
    Set<Integer> set = new HashSet<>(){{
        add(3);
        add(1);
        add(2);
    }};

    assertThat(set).contains(1, 2, 3);
}

Conclusion

In this tutorial, we learned some ways to create and initialize a HashSet in Java. You can find the full source code as below


Share to social

Van N.

Van N. is a software engineer, creator of HelloKoding. He loves coding, blogging, and traveling. You may find him on GitHub and LinkedIn