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
This commit is contained in:
micropatel 2018-07-15 17:03:26 -03:00 committed by maibin
parent e3bd6d493a
commit 61e6d9a3cd
1 changed files with 81 additions and 0 deletions

View File

@ -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<String> set = new HashSet<>(Arrays.asList("a", "b", "c"));
assertEquals(3, set.size());
}
@Test
public void whenUsingJava_usingAnonymousClass_thenCorrectSize() {
Set<String> set = new HashSet<String>(){{
add("a");
add("b");
add("c");
}};
assertEquals(3, set.size());
}
@Test
public void whenUsingJava_creatingSingletonSet_thenCorrectSize() {
Set<String> set = Collections.singleton("a");
assertEquals(1, set.size());
}
public static final <T> Set<T> newHashSet(T... objs) {
Set<T> set = new HashSet<T>();
Collections.addAll(set, objs);
return set;
}
@Test
public void whenUsingJava_usingCustomStaticUtilMethod_thenCorrectSize() {
Set<String> set = newHashSet("a","b","c");
assertEquals(3, set.size());
}
@Test
public void whenUsingJava8_usingCollectOnStream_thenCorrectSize() {
Set<String> 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<String> 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<String> set = Sets.newHashSet("a", "b", "c");
assertEquals(3, set.size());
}
@Test
public void whenUsingGoogleGuava_createImmutableSet_thenCorrectSize() {
Set<String> set = ImmutableSet.of("a", "b", "c");
assertEquals(3, set.size());
}
}