From d0a56cfbe7d564a539c733b3a0f86fee7e11b580 Mon Sep 17 00:00:00 2001 From: "alexandru.borza" Date: Sun, 29 Jan 2023 01:53:53 +0200 Subject: [PATCH] initialize arraylist with null or zeros --- .../pom.xml | 32 ++++++++ .../InitializeArrayListWithNullOrZeros.java | 14 ++++ ...alizeArrayListWithNullOrZerosUnitTest.java | 75 +++++++++++++++++++ pom.xml | 1 + 4 files changed, 122 insertions(+) create mode 100644 core-java-modules/core-java-collections-array-list-2/pom.xml create mode 100644 core-java-modules/core-java-collections-array-list-2/src/main/java/com/baeldung/InitializeArrayListWithNullOrZeros.java create mode 100644 core-java-modules/core-java-collections-array-list-2/src/test/java/com/baeldung/InitializeArrayListWithNullOrZerosUnitTest.java diff --git a/core-java-modules/core-java-collections-array-list-2/pom.xml b/core-java-modules/core-java-collections-array-list-2/pom.xml new file mode 100644 index 0000000000..ca29a0d14b --- /dev/null +++ b/core-java-modules/core-java-collections-array-list-2/pom.xml @@ -0,0 +1,32 @@ + + + + parent-modules + com.baeldung + 1.0.0-SNAPSHOT + ../../pom.xml + + 4.0.0 + + core-java-collections-array-list-2 + + + + org.apache.maven.plugins + maven-compiler-plugin + + 9 + 9 + + + + + + + 17 + 17 + + + diff --git a/core-java-modules/core-java-collections-array-list-2/src/main/java/com/baeldung/InitializeArrayListWithNullOrZeros.java b/core-java-modules/core-java-collections-array-list-2/src/main/java/com/baeldung/InitializeArrayListWithNullOrZeros.java new file mode 100644 index 0000000000..60cd18af98 --- /dev/null +++ b/core-java-modules/core-java-collections-array-list-2/src/main/java/com/baeldung/InitializeArrayListWithNullOrZeros.java @@ -0,0 +1,14 @@ +package com.baeldung; + +import java.util.ArrayList; + +public class InitializeArrayListWithNullOrZeros { + + public static void main(String[] args) { + + ArrayList arrayList = new ArrayList<>(); + for (int i = 0; i< 10; i++) { + arrayList.add(null); + } + } +} diff --git a/core-java-modules/core-java-collections-array-list-2/src/test/java/com/baeldung/InitializeArrayListWithNullOrZerosUnitTest.java b/core-java-modules/core-java-collections-array-list-2/src/test/java/com/baeldung/InitializeArrayListWithNullOrZerosUnitTest.java new file mode 100644 index 0000000000..71f8a23690 --- /dev/null +++ b/core-java-modules/core-java-collections-array-list-2/src/test/java/com/baeldung/InitializeArrayListWithNullOrZerosUnitTest.java @@ -0,0 +1,75 @@ +package com.baeldung; + +import org.junit.Test; +import org.junit.jupiter.api.Assertions; + +import java.util.*; +import java.util.stream.Collectors; +import java.util.stream.IntStream; +import java.util.stream.Stream; + + +public class InitializeArrayListWithNullOrZerosUnitTest { + + @Test + public void whenInitializingListWithNCopies_thenListIsCorrectlyPopulated() { + // when + ArrayList list = IntStream.of(new int[10]) + .boxed() + .collect(Collectors.toCollection(ArrayList::new)); + + // then + Assertions.assertEquals(10, list.size()); + Assertions.assertTrue(list.stream().allMatch(elem -> elem == 0)); + } + + @Test + public void whenInitializingListWithStream_thenListIsCorrectlyPopulated() { + + // when + ArrayList listWithZeros = Stream.generate(() -> 0) + .limit(10).collect(Collectors.toCollection(ArrayList::new)); + + ArrayList listWithNulls = Stream.generate(() -> null) + .limit(10).collect(Collectors.toCollection(ArrayList::new)); + + // then + Assertions.assertEquals(10, listWithZeros.size()); + Assertions.assertTrue(listWithZeros.stream().allMatch(elem -> elem == 0)); + + Assertions.assertEquals(10, listWithNulls.size()); + Assertions.assertTrue(listWithNulls.stream().allMatch(Objects::isNull)); + } + + @Test public void whenInitializingListWithIntStream_thenListIsCorrectlyPopulated() { + // when + ArrayList list = IntStream.of(new int[10]) + .boxed() + .collect(Collectors.toCollection(ArrayList::new)); + + // then + Assertions.assertEquals(10, list.size()); + Assertions.assertTrue(list.stream().allMatch(elem -> elem == 0)); } + + @Test + public void whenInitializingListWithAsList_thenListIsCorrectlyPopulated() { + // when + Integer[] integers = new Integer[10]; + Arrays.fill(integers, 0); + List integerList = new ArrayList<>(Arrays.asList(integers)); + + // then + Assertions.assertEquals(10, integerList.size()); + Assertions.assertTrue(integerList.stream().allMatch(elem -> elem == 0)); + } + + @Test + public void whenInitializingListWithVector_thenListIsCorrectlyPopulated() { + // when + List integerList = new Vector<>() {{setSize(10);}}; + + // then + Assertions.assertEquals(10, integerList.size()); + Assertions.assertTrue(integerList.stream().allMatch(Objects::isNull)); + } +} diff --git a/pom.xml b/pom.xml index 0b19c6eee6..cd3ae35b15 100644 --- a/pom.xml +++ b/pom.xml @@ -1134,6 +1134,7 @@ core-java-modules/core-java-collections-set core-java-modules/core-java-collections-list-4 core-java-modules/core-java-collections-array-list + core-java-modules/core-java-collections-array-list-2 core-java-modules/core-java-collections-maps-4 core-java-modules/core-java-collections-maps-5 core-java-modules/core-java-concurrency-simple