diff --git a/core-java-modules/core-java-char/src/test/java/com/baeldung/alphabetgeneration/GenerationOfCharactersUsingForVariousWaysUnitTest.java b/core-java-modules/core-java-char/src/test/java/com/baeldung/alphabetgeneration/GenerationOfCharactersUsingForVariousWaysUnitTest.java new file mode 100644 index 0000000000..bda50bb136 --- /dev/null +++ b/core-java-modules/core-java-char/src/test/java/com/baeldung/alphabetgeneration/GenerationOfCharactersUsingForVariousWaysUnitTest.java @@ -0,0 +1,29 @@ +package com.baeldung.alphabetgeneration; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.IntStream; + +class GenerationOfCharactersUsingForVariousWaysUnitTest { + @Test + void whenUsingForLoop_thenGenerateCharacters(){ + final List allCapitalCharacters = Arrays.asList('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'); + List characters = new ArrayList<>(); + for (char character = 'A'; character <= 'Z'; character++) { + characters.add(character); + } + Assertions.assertEquals(characters, allCapitalCharacters); + } + + @Test + void whenUsingStreams_thenGenerateCharacters() { + final List allCapitalCharacters = Arrays.asList('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'); + final List characters = IntStream.rangeClosed('A', 'Z').mapToObj(c -> (char) c).collect(Collectors.toList()); + Assertions.assertEquals(characters, allCapitalCharacters); + } +}