From 61e6d9a3cd21370c3b27422b18d56013a1ad9982 Mon Sep 17 00:00:00 2001 From: micropatel <31759369+micropatel@users.noreply.github.com> Date: Sun, 15 Jul 2018 17:03:26 -0300 Subject: [PATCH] BAEL-1985: Added Examples showing how to Initialize HashSet when it's constructed (#4715) * Added Class for Initalizing HahsSet * Updated Class name * Delete InitializingSetTest.java --- .../java/set/HashSetInitalizingUnitTest.java | 81 +++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 core-java-collections/src/test/java/com/baeldung/java/set/HashSetInitalizingUnitTest.java diff --git a/core-java-collections/src/test/java/com/baeldung/java/set/HashSetInitalizingUnitTest.java b/core-java-collections/src/test/java/com/baeldung/java/set/HashSetInitalizingUnitTest.java new file mode 100644 index 0000000000..13df09b597 --- /dev/null +++ b/core-java-collections/src/test/java/com/baeldung/java/set/HashSetInitalizingUnitTest.java @@ -0,0 +1,81 @@ +package com.baeldung.java.set; + +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Sets; +import org.junit.jupiter.api.Test; + +import java.util.Arrays; +import java.util.Collections; +import java.util.HashSet; +import java.util.Set; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import static org.junit.Assert.assertEquals; + +public class HashSetInitalizingUnitTest { + @Test + public void whenUsingJava_usingArraysStaticMethod_thenCorrectSize() { + Set set = new HashSet<>(Arrays.asList("a", "b", "c")); + assertEquals(3, set.size()); + } + + @Test + public void whenUsingJava_usingAnonymousClass_thenCorrectSize() { + Set set = new HashSet(){{ + add("a"); + add("b"); + add("c"); + }}; + assertEquals(3, set.size()); + } + + @Test + public void whenUsingJava_creatingSingletonSet_thenCorrectSize() { + Set set = Collections.singleton("a"); + assertEquals(1, set.size()); + } + + public static final Set newHashSet(T... objs) { + Set set = new HashSet(); + Collections.addAll(set, objs); + return set; + } + + @Test + public void whenUsingJava_usingCustomStaticUtilMethod_thenCorrectSize() { + Set set = newHashSet("a","b","c"); + assertEquals(3, set.size()); + } + + @Test + public void whenUsingJava8_usingCollectOnStream_thenCorrectSize() { + Set set = Stream.of("a", "b", "c").collect(Collectors.toSet()); + assertEquals(3, set.size()); + } + @Test + public void whenUsingJava8_fromStringArray_thenCorrectSize() { + String[] stringArray = {"a","b","c"}; + Set set = Arrays.stream(stringArray).collect(Collectors.toCollection(HashSet::new)); + assertEquals(3, set.size()); + } + + // Requires Java9 - uncomment if you are using Java 9 or higher + /*@Test + public void whenUsingJava9_usingCollectOnStream_thenCorrectSize() { + Set set = Set.of("a", "b", "c"); + assertEquals(3, set.size()); + }*/ + + @Test + public void whenUsingGoogleGuava_createMutableSet_thenCorrectSize() { + Set set = Sets.newHashSet("a", "b", "c"); + assertEquals(3, set.size()); + } + + @Test + public void whenUsingGoogleGuava_createImmutableSet_thenCorrectSize() { + Set set = ImmutableSet.of("a", "b", "c"); + assertEquals(3, set.size()); + } +}