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 1/2] 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()); + } +} From f68615d772fcd9118ab68ca42da3bf43ce82e7a0 Mon Sep 17 00:00:00 2001 From: Felipe Santiago Corro Date: Mon, 16 Jul 2018 03:41:08 -0300 Subject: [PATCH 2/2] Copy list to another list examples (#4725) --- .../java10/list/CopyListServiceUnitTest.java | 14 ++ .../com/baeldung/list/CopyListService.java | 73 ++++++++++ .../main/java/com/baeldung/list/Flower.java | 28 ++++ .../list/CopyListServiceUnitTest.java | 129 ++++++++++++++++++ 4 files changed, 244 insertions(+) create mode 100644 core-java-10/src/test/java/com/baeldung/java10/list/CopyListServiceUnitTest.java create mode 100644 core-java-8/src/main/java/com/baeldung/list/CopyListService.java create mode 100644 core-java-8/src/main/java/com/baeldung/list/Flower.java create mode 100644 core-java-8/src/test/java/com/baeldung/list/CopyListServiceUnitTest.java diff --git a/core-java-10/src/test/java/com/baeldung/java10/list/CopyListServiceUnitTest.java b/core-java-10/src/test/java/com/baeldung/java10/list/CopyListServiceUnitTest.java new file mode 100644 index 0000000000..f529e219a6 --- /dev/null +++ b/core-java-10/src/test/java/com/baeldung/java10/list/CopyListServiceUnitTest.java @@ -0,0 +1,14 @@ +package com.baeldung.java10.list; + +import org.junit.Test; + +import java.util.Arrays; +import java.util.List; + +public class CopyListServiceUnitTest { + + @Test(expected = UnsupportedOperationException.class) + public void whenModifyCopyOfList_thenThrowsException() { + List copyList = List.copyOf(Arrays.asList(1, 2, 3, 4)); + } +} diff --git a/core-java-8/src/main/java/com/baeldung/list/CopyListService.java b/core-java-8/src/main/java/com/baeldung/list/CopyListService.java new file mode 100644 index 0000000000..55d5bb9379 --- /dev/null +++ b/core-java-8/src/main/java/com/baeldung/list/CopyListService.java @@ -0,0 +1,73 @@ +package com.baeldung.list; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Optional; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +public class CopyListService { + + public List copyListByConstructor(List source) { + return new ArrayList(source); + } + + public List copyListByConstructorAndEditOneFlowerInTheNewList(List source) { + List flowers = new ArrayList<>(source); + if(flowers.size() > 0) { + flowers.get(0).setPetals(flowers.get(0).getPetals() * 3); + } + + return flowers; + } + + public List copyListByAddAllMethod(List source) { + List flowers = new ArrayList<>(); + flowers.addAll(source); + return flowers; + } + + public List copyListByAddAllMethodAndEditOneFlowerInTheNewList(List source) { + List flowers = new ArrayList<>(); + flowers.addAll(source); + + if(flowers.size() > 0) { + flowers.get(0).setPetals(flowers.get(0).getPetals() * 3); + } + + return flowers; + } + + public List copyListByCopyMethod(List source, List dest) { + Collections.copy(dest, source); + return dest; + } + + public List copyListByStream(List source) { + return source.stream().collect(Collectors.toList()); + } + + public List copyListByStreamAndSkipFirstElement(List source) { + return source.stream().skip(1).collect(Collectors.toList()); + } + + public List copyListByStreamWithFilter(List source, Integer moreThanPetals) { + return source.stream().filter(f -> f.getPetals() > moreThanPetals).collect(Collectors.toList()); + } + + public List copyListByStreamWithOptional(List source) { + return Optional.ofNullable(source) + .map(List::stream) + .orElseGet(Stream::empty) + .collect(Collectors.toList()); + } + + public List copyListByStreamWithOptionalAndSkip(List source) { + return Optional.ofNullable(source) + .map(List::stream) + .orElseGet(Stream::empty) + .skip(1) + .collect(Collectors.toList()); + } +} diff --git a/core-java-8/src/main/java/com/baeldung/list/Flower.java b/core-java-8/src/main/java/com/baeldung/list/Flower.java new file mode 100644 index 0000000000..29c6a78326 --- /dev/null +++ b/core-java-8/src/main/java/com/baeldung/list/Flower.java @@ -0,0 +1,28 @@ +package com.baeldung.list; + +public class Flower { + + private String name; + private int petals; + + public Flower(String name, int petals) { + this.name = name; + this.petals = petals; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public int getPetals() { + return petals; + } + + public void setPetals(int petals) { + this.petals = petals; + } +} diff --git a/core-java-8/src/test/java/com/baeldung/list/CopyListServiceUnitTest.java b/core-java-8/src/test/java/com/baeldung/list/CopyListServiceUnitTest.java new file mode 100644 index 0000000000..348747111f --- /dev/null +++ b/core-java-8/src/test/java/com/baeldung/list/CopyListServiceUnitTest.java @@ -0,0 +1,129 @@ +package com.baeldung.list; + +import org.junit.Before; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import static org.junit.Assert.*; + +public class CopyListServiceUnitTest { + + List flowers; + + private CopyListService copyListService; + + @Before + public void init() { + this.copyListService = new CopyListService(); + this.flowers = new ArrayList<>(); + + Flower poppy = new Flower("Poppy", 12); + flowers.add(poppy); + Flower anemone = new Flower("Anemone", 8); + flowers.add(anemone); + Flower catmint = new Flower("Catmint", 12); + flowers.add(catmint); + Flower diascia = new Flower("Diascia", 5); + flowers.add(diascia); + Flower iris = new Flower("Iris", 3); + flowers.add(iris); + Flower pansy = new Flower("Pansy", 5); + flowers.add(pansy); + } + + @Test + public void givenAList_whenListDoesNotHaveNullElements_thenReturnAnotherListWithTheSameElementsByConstructor() { + List copy = copyListService.copyListByConstructor(flowers); + assertEquals(copy.size(), flowers.size()); + assertTrue(copy.containsAll(flowers)); + } + + @Test + public void givenAList_whenListDoesNotHaveNullElements_thenReturnAnotherListWithOneModifiedElementByConstructor() { + List copy = copyListService.copyListByConstructorAndEditOneFlowerInTheNewList(flowers); + assertEquals(copy.size(), flowers.size()); + assertTrue(copy.containsAll(flowers)); + } + + @Test + public void givenAList_whenListDoesNotHaveNullElements_thenReturnAnotherListWithTheSameElementsByAddAllmethod() { + List copy = copyListService.copyListByAddAllMethod(flowers); + assertEquals(copy.size(), flowers.size()); + assertTrue(copy.containsAll(flowers)); + } + + @Test + public void givenAList_whenListDoesNotHaveNullElements_thenReturnAnotherListWithOneModifiedElementByAddAllmethod() { + List copy = copyListService.copyListByAddAllMethodAndEditOneFlowerInTheNewList(flowers); + assertEquals(copy.size(), flowers.size()); + assertTrue(copy.containsAll(flowers)); + } + + @Test + public void givenAList_whenListsHaveSameSize_thenReturnAnotherListWithTheSameElementsByCopyMethod() { + List source = Arrays.asList(1,2,3); + List dest = Arrays.asList(4,5,6); + + dest = copyListService.copyListByCopyMethod(source, dest); + assertEquals(dest.size(), source.size()); + assertTrue(dest.containsAll(source)); + } + + @Test + public void givenAList_whenListsHaveDifferentSize_thenReturnAnotherListWithTheSameElementsByCopyMethod() { + List source = Arrays.asList(1,2,3); + List dest = Arrays.asList(5,6,7,8,9,10); + + dest = copyListService.copyListByCopyMethod(source, dest); + assertNotEquals(dest.size(), source.size()); + assertTrue(dest.containsAll(source)); + } + + @Test + public void givenAList_whenListDoesNotHaveNullElements_thenReturnAnotherListWithTheSameElementsByStreamProcess() { + List copy = copyListService.copyListByStream(flowers); + assertEquals(copy.size(), flowers.size()); + assertTrue(copy.containsAll(flowers)); + } + + @Test + public void givenAList_whenListDoesNotHaveNullElements_thenReturnAnotherListWithOneElementLessByStreamProcess() { + List copy = copyListService.copyListByStreamAndSkipFirstElement(flowers); + assertNotEquals(copy.size(), flowers.size()); + assertEquals(copy.size() + 1, flowers.size()); + assertFalse(copy.containsAll(flowers)); + } + + @Test + public void givenAList_whenListDoesNotHaveNullElements_thenReturnAnotherListWithFilterElementsByStreamProcess() { + List copy = copyListService.copyListByStreamWithFilter(flowers, 5); + assertNotEquals(copy.size(), flowers.size()); + assertEquals(copy.size() + 3, flowers.size()); + assertFalse(copy.containsAll(flowers)); + } + + @Test + public void givenAList_whenListIsNull_thenReturnEmptyListByStreamProcess() { + List copy = copyListService.copyListByStreamWithOptional(null); + assertNotNull(copy); + assertEquals(copy.size(), 0); + } + + @Test + public void givenAList_whenListIsNotNull_thenReturnAnotherListWithTheElementsByStreamProcess() { + List copy = copyListService.copyListByStreamWithOptional(flowers); + assertEquals(copy.size(), flowers.size()); + assertTrue(copy.containsAll(flowers)); + } + + @Test + public void givenAList_whenListIsNotNull_thenReturnAnotherListWithOneElementLessByStreamProcess() { + List copy = copyListService.copyListByStreamWithOptionalAndSkip(flowers); + assertNotEquals(copy.size(), flowers.size()); + assertEquals(copy.size() + 1, flowers.size()); + assertFalse(copy.containsAll(flowers)); + } +}