initialize arraylist with null or zeros
This commit is contained in:
		
							parent
							
								
									7605f8474c
								
							
						
					
					
						commit
						d0a56cfbe7
					
				
							
								
								
									
										32
									
								
								core-java-modules/core-java-collections-array-list-2/pom.xml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										32
									
								
								core-java-modules/core-java-collections-array-list-2/pom.xml
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,32 @@ | ||||
| <?xml version="1.0" encoding="UTF-8"?> | ||||
| <project xmlns="http://maven.apache.org/POM/4.0.0" | ||||
|          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||||
|          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||||
|     <parent> | ||||
|         <artifactId>parent-modules</artifactId> | ||||
|         <groupId>com.baeldung</groupId> | ||||
|         <version>1.0.0-SNAPSHOT</version> | ||||
|         <relativePath>../../pom.xml</relativePath> | ||||
|     </parent> | ||||
|     <modelVersion>4.0.0</modelVersion> | ||||
| 
 | ||||
|     <artifactId>core-java-collections-array-list-2</artifactId> | ||||
|     <build> | ||||
|         <plugins> | ||||
|             <plugin> | ||||
|                 <groupId>org.apache.maven.plugins</groupId> | ||||
|                 <artifactId>maven-compiler-plugin</artifactId> | ||||
|                 <configuration> | ||||
|                     <source>9</source> | ||||
|                     <target>9</target> | ||||
|                 </configuration> | ||||
|             </plugin> | ||||
|         </plugins> | ||||
|     </build> | ||||
| 
 | ||||
|     <properties> | ||||
|         <maven.compiler.source>17</maven.compiler.source> | ||||
|         <maven.compiler.target>17</maven.compiler.target> | ||||
|     </properties> | ||||
| 
 | ||||
| </project> | ||||
| @ -0,0 +1,14 @@ | ||||
| package com.baeldung; | ||||
| 
 | ||||
| import java.util.ArrayList; | ||||
| 
 | ||||
| public class InitializeArrayListWithNullOrZeros { | ||||
| 
 | ||||
|      public static void main(String[] args) { | ||||
| 
 | ||||
|          ArrayList<Integer> arrayList = new ArrayList<>(); | ||||
|          for (int i = 0; i< 10; i++) { | ||||
|              arrayList.add(null); | ||||
|          } | ||||
|      } | ||||
| } | ||||
| @ -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<Integer> 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<Integer> listWithZeros = Stream.generate(() -> 0) | ||||
|                 .limit(10).collect(Collectors.toCollection(ArrayList::new)); | ||||
| 
 | ||||
|         ArrayList<Object> 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<Integer> 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<Integer> 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<Integer> integerList = new Vector<>() {{setSize(10);}}; | ||||
| 
 | ||||
|         // then | ||||
|         Assertions.assertEquals(10, integerList.size()); | ||||
|         Assertions.assertTrue(integerList.stream().allMatch(Objects::isNull)); | ||||
|     } | ||||
| } | ||||
							
								
								
									
										1
									
								
								pom.xml
									
									
									
									
									
								
							
							
						
						
									
										1
									
								
								pom.xml
									
									
									
									
									
								
							| @ -1134,6 +1134,7 @@ | ||||
|                  <module>core-java-modules/core-java-collections-set</module> | ||||
|                  <module>core-java-modules/core-java-collections-list-4</module> | ||||
|                  <module>core-java-modules/core-java-collections-array-list</module> | ||||
|                  <module>core-java-modules/core-java-collections-array-list-2</module> | ||||
|                  <module>core-java-modules/core-java-collections-maps-4</module> | ||||
|                  <module>core-java-modules/core-java-collections-maps-5</module> | ||||
|                  <module>core-java-modules/core-java-concurrency-simple</module> | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user