diff --git a/core-java/pom.xml b/core-java/pom.xml index 2f3bf4bb39..0cafeae16a 100644 --- a/core-java/pom.xml +++ b/core-java/pom.xml @@ -1,307 +1,313 @@ - 4.0.0 - com.baeldung - core-java - 0.1.0-SNAPSHOT - jar + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + 4.0.0 + com.baeldung + core-java + 0.1.0-SNAPSHOT + jar - core-java + core-java - + - - - net.sourceforge.collections - collections-generic - ${collections-generic.version} - - - com.google.guava - guava - ${guava.version} - - - - org.apache.commons - commons-collections4 - ${commons-collections4.version} - - - - commons-io - commons-io - ${commons-io.version} - - - - org.apache.commons - commons-lang3 - ${commons-lang3.version} - - - - org.apache.commons - commons-math3 - ${commons-math3.version} - - - - org.bouncycastle - bcprov-jdk15on - ${bouncycastle.version} - - - - org.unix4j - unix4j-command - 0.4 - - - - com.googlecode.grep4j - grep4j - 1.8.7 + + + net.sourceforge.collections + collections-generic + ${collections-generic.version} + + + com.google.guava + guava + ${guava.version} - - + + org.apache.commons + commons-collections4 + ${commons-collections4.version} + - - com.fasterxml.jackson.core - jackson-databind - ${jackson.version} - + + commons-io + commons-io + ${commons-io.version} + - + + org.apache.commons + commons-lang3 + ${commons-lang3.version} + - - org.slf4j - slf4j-api - ${org.slf4j.version} - - - ch.qos.logback - logback-classic - ${logback.version} - - - - org.slf4j - jcl-over-slf4j - ${org.slf4j.version} - - - - org.slf4j - log4j-over-slf4j - ${org.slf4j.version} - - - org.projectlombok - lombok - 1.16.12 - provided - + + org.apache.commons + commons-math3 + ${commons-math3.version} + - + + org.bouncycastle + bcprov-jdk15on + ${bouncycastle.version} + - - junit - junit - ${junit.version} - test - + + org.unix4j + unix4j-command + 0.4 + - - org.hamcrest - hamcrest-core - ${org.hamcrest.version} - test - - - org.hamcrest - hamcrest-library - ${org.hamcrest.version} - test - + + com.googlecode.grep4j + grep4j + 1.8.7 + + - - org.assertj - assertj-core - ${assertj.version} - test - + - - org.testng - testng - ${testng.version} - test - + + com.fasterxml.jackson.core + jackson-databind + ${jackson.version} + - - org.mockito - mockito-core - ${mockito.version} - test - + - - commons-codec - commons-codec - ${commons-codec.version} - + + org.slf4j + slf4j-api + ${org.slf4j.version} + + + ch.qos.logback + logback-classic + ${logback.version} + + + + org.slf4j + jcl-over-slf4j + ${org.slf4j.version} + + + + org.slf4j + log4j-over-slf4j + ${org.slf4j.version} + + + org.projectlombok + lombok + 1.16.12 + provided + - + - - core-java - - - src/main/resources - true - - + + org.hamcrest + hamcrest-all + 1.3 + test + - + + junit + junit + ${junit.version} + test + - - org.apache.maven.plugins - maven-compiler-plugin - ${maven-compiler-plugin.version} - - 1.8 - 1.8 - - + + org.hamcrest + hamcrest-core + ${org.hamcrest.version} + test + + + org.hamcrest + hamcrest-library + ${org.hamcrest.version} + test + - - org.apache.maven.plugins - maven-surefire-plugin - ${maven-surefire-plugin.version} - - - **/*IntegrationTest.java + + org.assertj + assertj-core + ${assertj.version} + test + + + + org.testng + testng + ${testng.version} + test + + + + org.mockito + mockito-core + ${mockito.version} + test + + + + commons-codec + commons-codec + ${commons-codec.version} + + + + + + core-java + + + src/main/resources + true + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + ${maven-compiler-plugin.version} + + 1.8 + 1.8 + + + + + org.apache.maven.plugins + maven-surefire-plugin + ${maven-surefire-plugin.version} + + + **/*IntegrationTest.java **/*LongRunningUnitTest.java **/*ManualTest.java - - - + + + - - org.apache.maven.plugins - maven-dependency-plugin - - - copy-dependencies - prepare-package - - copy-dependencies - - - ${project.build.directory}/libs - - - - + + org.apache.maven.plugins + maven-dependency-plugin + + + copy-dependencies + prepare-package + + copy-dependencies + + + ${project.build.directory}/libs + + + + - - org.apache.maven.plugins - maven-jar-plugin - - - - true - libs/ - org.baeldung.executable.ExecutableMavenJar - - - - + + org.apache.maven.plugins + maven-jar-plugin + + + + true + libs/ + org.baeldung.executable.ExecutableMavenJar + + + + - - org.apache.maven.plugins - maven-assembly-plugin - - - package - - single - - - - - org.baeldung.executable.ExecutableMavenJar - - - - jar-with-dependencies - - - - - + + org.apache.maven.plugins + maven-assembly-plugin + + + package + + single + + + + + org.baeldung.executable.ExecutableMavenJar + + + + jar-with-dependencies + + + + + - - org.apache.maven.plugins - maven-shade-plugin - - - - shade - - - true - - - org.baeldung.executable.ExecutableMavenJar - - - - - - + + org.apache.maven.plugins + maven-shade-plugin + + + + shade + + + true + + + org.baeldung.executable.ExecutableMavenJar + + + + + + - - com.jolira - onejar-maven-plugin - - - - org.baeldung.executable.ExecutableMavenJar - true - ${project.build.finalName}-onejar.${project.packaging} - - - one-jar - - - - + + com.jolira + onejar-maven-plugin + + + + org.baeldung.executable.ExecutableMavenJar + true + ${project.build.finalName}-onejar.${project.packaging} + + + one-jar + + + + - - org.springframework.boot - spring-boot-maven-plugin - - - - repackage - - - spring-boot - org.baeldung.executable.ExecutableMavenJar - - - - - - + + org.springframework.boot + spring-boot-maven-plugin + + + + repackage + + + spring-boot + org.baeldung.executable.ExecutableMavenJar + + + + - - - + + + + + integration @@ -336,35 +342,35 @@ - - - 2.8.5 + + + 2.8.5 - - 1.7.21 - 1.1.7 + + 1.7.21 + 1.1.7 - - 19.0 - 3.5 - 1.55 - 1.10 - 3.6.1 - 2.5 - 4.1 - 4.01 + + 19.0 + 3.5 + 1.55 + 1.10 + 3.6.1 + 2.5 + 4.1 + 4.01 - - 1.3 - 4.12 - 1.10.19 - 6.10 - 3.6.1 + + 1.3 + 4.12 + 1.10.19 + 6.10 + 3.6.1 - - 3.6.0 - 2.19.1 + + 3.6.0 + 2.19.1 - + \ No newline at end of file diff --git a/core-java/src/main/java/com/baeldung/algorithms/City.java b/core-java/src/main/java/com/baeldung/algorithms/City.java index 1a96dc759d..32335cda80 100644 --- a/core-java/src/main/java/com/baeldung/algorithms/City.java +++ b/core-java/src/main/java/com/baeldung/algorithms/City.java @@ -5,18 +5,18 @@ import lombok.Data; @Data public class City { - private int x; - private int y; + private int x; + private int y; - public City() { - this.x = (int) (Math.random() * 500); - this.y = (int) (Math.random() * 500); - } + public City() { + this.x = (int) (Math.random() * 500); + this.y = (int) (Math.random() * 500); + } - public double distanceToCity(City city) { - int x = Math.abs(getX() - city.getX()); - int y = Math.abs(getY() - city.getY()); - return Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2)); - } + public double distanceToCity(City city) { + int x = Math.abs(getX() - city.getX()); + int y = Math.abs(getY() - city.getY()); + return Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2)); + } } diff --git a/core-java/src/main/java/com/baeldung/algorithms/SimulatedAnnealing.java b/core-java/src/main/java/com/baeldung/algorithms/SimulatedAnnealing.java index b62e861399..3ccba9b3e9 100644 --- a/core-java/src/main/java/com/baeldung/algorithms/SimulatedAnnealing.java +++ b/core-java/src/main/java/com/baeldung/algorithms/SimulatedAnnealing.java @@ -2,40 +2,39 @@ package com.baeldung.algorithms; public class SimulatedAnnealing { - private static Travel travel = new Travel(10); - - public static double simulateAnnealing(double startingTemperature, int numberOfIterations, double coolingRate) { - System.out.println("Starting SA with temperature: " + startingTemperature + ", # of iterations: " - + numberOfIterations + " and colling rate: " + coolingRate); - double t = startingTemperature; - travel.generateInitialTravel(); - double bestDistance = travel.getDistance(); - System.out.println("Initial distance of travel: " + bestDistance); - Travel bestSolution = travel; - Travel currentSolution = bestSolution; + private static Travel travel = new Travel(10); - for (int i = 0; i < numberOfIterations; i++) { - if (t > 0.1) { - currentSolution.swapCities(); - double currentDistance = currentSolution.getDistance(); - if (currentDistance == 0) - continue; - if (currentDistance < bestDistance) { - bestDistance = currentDistance; - } else if (Math.exp((currentDistance - bestDistance) / t) < Math.random()) { - currentSolution.revertSwap(); - } - t *= coolingRate; - } - if (i % 100 == 0) { - System.out.println("Iteration #" + i); - } - } - return bestDistance; - } + public static double simulateAnnealing(double startingTemperature, int numberOfIterations, double coolingRate) { + System.out.println("Starting SA with temperature: " + startingTemperature + ", # of iterations: " + numberOfIterations + " and colling rate: " + coolingRate); + double t = startingTemperature; + travel.generateInitialTravel(); + double bestDistance = travel.getDistance(); + System.out.println("Initial distance of travel: " + bestDistance); + Travel bestSolution = travel; + Travel currentSolution = bestSolution; - public static void main(String[] args) { - System.out.println("Optimized distance for travel: " + simulateAnnealing(10, 10000, 0.9)); - } + for (int i = 0; i < numberOfIterations; i++) { + if (t > 0.1) { + currentSolution.swapCities(); + double currentDistance = currentSolution.getDistance(); + if (currentDistance == 0) + continue; + if (currentDistance < bestDistance) { + bestDistance = currentDistance; + } else if (Math.exp((currentDistance - bestDistance) / t) < Math.random()) { + currentSolution.revertSwap(); + } + t *= coolingRate; + } + if (i % 100 == 0) { + System.out.println("Iteration #" + i); + } + } + return bestDistance; + } + + public static void main(String[] args) { + System.out.println("Optimized distance for travel: " + simulateAnnealing(10, 10000, 0.9)); + } } diff --git a/core-java/src/main/java/com/baeldung/algorithms/Travel.java b/core-java/src/main/java/com/baeldung/algorithms/Travel.java index 9921b2516e..6e6059b3eb 100644 --- a/core-java/src/main/java/com/baeldung/algorithms/Travel.java +++ b/core-java/src/main/java/com/baeldung/algorithms/Travel.java @@ -8,53 +8,53 @@ import lombok.Data; @Data public class Travel { - private ArrayList travel = new ArrayList<>(); - private ArrayList previousTravel = new ArrayList<>(); + private ArrayList travel = new ArrayList<>(); + private ArrayList previousTravel = new ArrayList<>(); - public Travel(int numberOfCities) { - for (int i = 0; i < numberOfCities; i++) { - travel.add(new City()); - } - } + public Travel(int numberOfCities) { + for (int i = 0; i < numberOfCities; i++) { + travel.add(new City()); + } + } - public void generateInitialTravel() { - if (travel.isEmpty()) - new Travel(10); - Collections.shuffle(travel); - } + public void generateInitialTravel() { + if (travel.isEmpty()) + new Travel(10); + Collections.shuffle(travel); + } - public void swapCities() { - int a = generateRandomIndex(); - int b = generateRandomIndex(); - previousTravel = travel; - travel.set(a, travel.get(b)); - } + public void swapCities() { + int a = generateRandomIndex(); + int b = generateRandomIndex(); + previousTravel = travel; + travel.set(a, travel.get(b)); + } - public void revertSwap() { - travel = previousTravel; - } + public void revertSwap() { + travel = previousTravel; + } - private int generateRandomIndex() { - return (int) (Math.random() * travel.size()); - } + private int generateRandomIndex() { + return (int) (Math.random() * travel.size()); + } - public City getCity(int index) { - return travel.get(index); - } + public City getCity(int index) { + return travel.get(index); + } - public int getDistance() { - int distance = 0; - for (int index = 0; index < travel.size(); index++) { - City starting = getCity(index); - City destination; - if (index + 1 < travel.size()) { - destination = getCity(index + 1); - } else { - destination = getCity(0); - } - distance += starting.distanceToCity(destination); - } - return distance; - } + public int getDistance() { + int distance = 0; + for (int index = 0; index < travel.size(); index++) { + City starting = getCity(index); + City destination; + if (index + 1 < travel.size()) { + destination = getCity(index + 1); + } else { + destination = getCity(0); + } + distance += starting.distanceToCity(destination); + } + return distance; + } } diff --git a/core-java/src/test/java/com/baeldung/algorithms/SimulatedAnnealingTest.java b/core-java/src/test/java/com/baeldung/algorithms/SimulatedAnnealingTest.java index 06b599dede..3fe3650041 100644 --- a/core-java/src/test/java/com/baeldung/algorithms/SimulatedAnnealingTest.java +++ b/core-java/src/test/java/com/baeldung/algorithms/SimulatedAnnealingTest.java @@ -5,9 +5,9 @@ import org.junit.Test; public class SimulatedAnnealingTest { - @Test - public void testSimulateAnnealing() { - Assert.assertTrue(SimulatedAnnealing.simulateAnnealing(10, 1000, 0.9) > 0); - } + @Test + public void testSimulateAnnealing() { + Assert.assertTrue(SimulatedAnnealing.simulateAnnealing(10, 1000, 0.9) > 0); + } } diff --git a/core-java/src/test/java/com/baeldung/encoderdecoder/EncoderDecoderUnitTest.java b/core-java/src/test/java/com/baeldung/encoderdecoder/EncoderDecoderUnitTest.java index 08f4e0b4bd..da615eef6f 100644 --- a/core-java/src/test/java/com/baeldung/encoderdecoder/EncoderDecoderUnitTest.java +++ b/core-java/src/test/java/com/baeldung/encoderdecoder/EncoderDecoderUnitTest.java @@ -22,7 +22,6 @@ public class EncoderDecoderUnitTest { private static final String testUrl = "http://www.baeldung.com?key1=value+1&key2=value%40%21%242&key3=value%253"; private static final String testUrlWithPath = "http://www.baeldung.com/path+1?key1=value+1&key2=value%40%21%242&key3=value%253"; - private String encodeValue(String value) { String encoded = null; try { @@ -59,9 +58,7 @@ public class EncoderDecoderUnitTest { requestParams.put("key2", "value@!$2"); requestParams.put("key3", "value%3"); - String encodedURL = requestParams.keySet().stream() - .map(key -> key + "=" + encodeValue(requestParams.get(key))) - .collect(joining("&", "http://www.baeldung.com?", "")); + String encodedURL = requestParams.keySet().stream().map(key -> key + "=" + encodeValue(requestParams.get(key))).collect(joining("&", "http://www.baeldung.com?", "")); Assert.assertThat(testUrl, is(encodedURL)); } @@ -103,12 +100,9 @@ public class EncoderDecoderUnitTest { String path = "path+1"; - String encodedURL = requestParams.keySet().stream() - .map(key -> key + "=" + encodeValue(requestParams.get(key))) - .collect(joining("&", "http://www.baeldung.com/" + encodePath(path) + "?", "")); + String encodedURL = requestParams.keySet().stream().map(key -> key + "=" + encodeValue(requestParams.get(key))).collect(joining("&", "http://www.baeldung.com/" + encodePath(path) + "?", "")); Assert.assertThat(testUrlWithPath, is(encodedURL)); } - } diff --git a/core-java/src/test/java/com/baeldung/java8/Java8CollectionCleanupUnitTest.java b/core-java/src/test/java/com/baeldung/java8/Java8CollectionCleanupUnitTest.java index 4b900c31a7..ef7b642f89 100644 --- a/core-java/src/test/java/com/baeldung/java8/Java8CollectionCleanupUnitTest.java +++ b/core-java/src/test/java/com/baeldung/java8/Java8CollectionCleanupUnitTest.java @@ -17,9 +17,7 @@ public class Java8CollectionCleanupUnitTest { @Test public void givenListContainsNulls_whenFilteringParallel_thenCorrect() { final List list = Lists.newArrayList(null, 1, 2, null, 3, null); - final List listWithoutNulls = list.parallelStream() - .filter(Objects::nonNull) - .collect(Collectors.toList()); + final List listWithoutNulls = list.parallelStream().filter(Objects::nonNull).collect(Collectors.toList()); assertThat(listWithoutNulls, hasSize(3)); } @@ -27,9 +25,7 @@ public class Java8CollectionCleanupUnitTest { @Test public void givenListContainsNulls_whenFilteringSerial_thenCorrect() { final List list = Lists.newArrayList(null, 1, 2, null, 3, null); - final List listWithoutNulls = list.stream() - .filter(Objects::nonNull) - .collect(Collectors.toList()); + final List listWithoutNulls = list.stream().filter(Objects::nonNull).collect(Collectors.toList()); assertThat(listWithoutNulls, hasSize(3)); } @@ -45,9 +41,7 @@ public class Java8CollectionCleanupUnitTest { @Test public void givenListContainsDuplicates_whenRemovingDuplicatesWithJava8_thenCorrect() { final List listWithDuplicates = Lists.newArrayList(1, 1, 2, 2, 3, 3); - final List listWithoutDuplicates = listWithDuplicates.parallelStream() - .distinct() - .collect(Collectors.toList()); + final List listWithoutDuplicates = listWithDuplicates.parallelStream().distinct().collect(Collectors.toList()); assertThat(listWithoutDuplicates, hasSize(3)); } diff --git a/core-java/src/test/java/com/baeldung/java8/JavaFileSizeUnitTest.java b/core-java/src/test/java/com/baeldung/java8/JavaFileSizeUnitTest.java index c4920c51f6..c640932d6f 100644 --- a/core-java/src/test/java/com/baeldung/java8/JavaFileSizeUnitTest.java +++ b/core-java/src/test/java/com/baeldung/java8/JavaFileSizeUnitTest.java @@ -18,13 +18,13 @@ public class JavaFileSizeUnitTest { @Before public void init() { final String separator = File.separator; - filePath = String.join(separator, new String[] {"src", "test", "resources", "testFolder", "sample_file_1.in"}); + filePath = String.join(separator, new String[] { "src", "test", "resources", "testFolder", "sample_file_1.in" }); } @Test public void whenGetFileSize_thenCorrect() { final File file = new File(filePath); - + final long size = getFileSize(file); assertEquals(EXPECTED_FILE_SIZE_IN_BYTES, size); @@ -34,16 +34,16 @@ public class JavaFileSizeUnitTest { public void whenGetFileSizeUsingNioApi_thenCorrect() throws IOException { final Path path = Paths.get(this.filePath); final FileChannel fileChannel = FileChannel.open(path); - + final long fileSize = fileChannel.size(); - + assertEquals(EXPECTED_FILE_SIZE_IN_BYTES, fileSize); } @Test public void whenGetFileSizeUsingApacheCommonsIO_thenCorrect() { final File file = new File(filePath); - + final long size = FileUtils.sizeOf(file); assertEquals(EXPECTED_FILE_SIZE_IN_BYTES, size); @@ -52,9 +52,9 @@ public class JavaFileSizeUnitTest { @Test public void whenGetReadableFileSize_thenCorrect() { final File file = new File(filePath); - + final long size = getFileSize(file); - + assertEquals(EXPECTED_FILE_SIZE_IN_BYTES + " bytes", FileUtils.byteCountToDisplaySize(size)); } diff --git a/core-java/src/test/java/com/baeldung/java8/optional/OptionalTest.java b/core-java/src/test/java/com/baeldung/java8/optional/OptionalTest.java index c0538931b0..bf2078186c 100644 --- a/core-java/src/test/java/com/baeldung/java8/optional/OptionalTest.java +++ b/core-java/src/test/java/com/baeldung/java8/optional/OptionalTest.java @@ -93,6 +93,7 @@ public class OptionalTest { boolean is2017 = yearOptional.filter(y -> y == 2017).isPresent(); assertFalse(is2017); } + @Test public void whenFiltersWithoutOptional_thenCorrect() { assertTrue(priceIsInRange1(new Modem(10.0))); @@ -121,12 +122,9 @@ public class OptionalTest { } public boolean priceIsInRange2(Modem modem2) { - return Optional.ofNullable(modem2) - .map(Modem::getPrice) - .filter(p -> p >= 10) - .filter(p -> p <= 15) - .isPresent(); + return Optional.ofNullable(modem2).map(Modem::getPrice).filter(p -> p >= 10).filter(p -> p <= 15).isPresent(); } + // Transforming Value With map() @Test public void givenOptional_whenMapWorks_thenCorrect() { diff --git a/core-java/src/test/java/com/baeldung/java8/unix/grep/GrepWithUnix4JTest.java b/core-java/src/test/java/com/baeldung/java8/unix/grep/GrepWithUnix4JTest.java index d6efdfdf5c..79ba2e0b17 100644 --- a/core-java/src/test/java/com/baeldung/java8/unix/grep/GrepWithUnix4JTest.java +++ b/core-java/src/test/java/com/baeldung/java8/unix/grep/GrepWithUnix4JTest.java @@ -13,46 +13,43 @@ import static org.unix4j.unix.Grep.*; import static org.unix4j.unix.cut.CutOption.*; public class GrepWithUnix4JTest { - - private File fileToGrep; + + private File fileToGrep; @Before public void init() { final String separator = File.separator; - final String filePath = String.join(separator, new String[] {"src", "test", "resources", "dictionary.in"}); + final String filePath = String.join(separator, new String[] { "src", "test", "resources", "dictionary.in" }); fileToGrep = new File(filePath); } - - @Test - public void whenGrepWithSimpleString_thenCorrect() { - int expectedLineCount = 4; - - //grep "NINETEEN" dictionary.txt - List lines = Unix4j.grep("NINETEEN", fileToGrep).toLineList(); - - assertEquals(expectedLineCount, lines.size()); - } - @Test - public void whenInverseGrepWithSimpleString_thenCorrect() { - int expectedLineCount = 178687; - - //grep -v "NINETEEN" dictionary.txt - List lines = grep(Options.v, "NINETEEN", fileToGrep). - toLineList(); - - assertEquals(expectedLineCount, lines.size()); - } + @Test + public void whenGrepWithSimpleString_thenCorrect() { + int expectedLineCount = 4; + // grep "NINETEEN" dictionary.txt + List lines = Unix4j.grep("NINETEEN", fileToGrep).toLineList(); - @Test - public void whenGrepWithRegex_thenCorrect() { - int expectedLineCount = 151; - - //grep -c ".*?NINE.*?" dictionary.txt - String patternCount = grep(Options.c, ".*?NINE.*?", fileToGrep). - cut(fields, ":", 1).toStringResult(); - - assertEquals(expectedLineCount, Integer.parseInt(patternCount)); - } + assertEquals(expectedLineCount, lines.size()); + } + + @Test + public void whenInverseGrepWithSimpleString_thenCorrect() { + int expectedLineCount = 178687; + + // grep -v "NINETEEN" dictionary.txt + List lines = grep(Options.v, "NINETEEN", fileToGrep).toLineList(); + + assertEquals(expectedLineCount, lines.size()); + } + + @Test + public void whenGrepWithRegex_thenCorrect() { + int expectedLineCount = 151; + + // grep -c ".*?NINE.*?" dictionary.txt + String patternCount = grep(Options.c, ".*?NINE.*?", fileToGrep).cut(fields, ":", 1).toStringResult(); + + assertEquals(expectedLineCount, Integer.parseInt(patternCount)); + } } diff --git a/core-java/src/test/java/org/baeldung/java/collections/JoinSplitCollectionsUnitTest.java b/core-java/src/test/java/org/baeldung/java/collections/JoinSplitCollectionsUnitTest.java index 16b3509925..c594529f41 100644 --- a/core-java/src/test/java/org/baeldung/java/collections/JoinSplitCollectionsUnitTest.java +++ b/core-java/src/test/java/org/baeldung/java/collections/JoinSplitCollectionsUnitTest.java @@ -15,8 +15,7 @@ public class JoinSplitCollectionsUnitTest { public void whenJoiningTwoArrays_thenJoined() { String[] animals1 = new String[] { "Dog", "Cat" }; String[] animals2 = new String[] { "Bird", "Cow" }; - String[] result = Stream.concat( - Arrays.stream(animals1), Arrays.stream(animals2)).toArray(String[]::new); + String[] result = Stream.concat(Arrays.stream(animals1), Arrays.stream(animals2)).toArray(String[]::new); assertArrayEquals(result, new String[] { "Dog", "Cat", "Bird", "Cow" }); } @@ -25,9 +24,7 @@ public class JoinSplitCollectionsUnitTest { public void whenJoiningTwoCollections_thenJoined() { Collection collection1 = Arrays.asList("Dog", "Cat"); Collection collection2 = Arrays.asList("Bird", "Cow", "Moose"); - Collection result = Stream.concat( - collection1.stream(), collection2.stream()) - .collect(Collectors.toList()); + Collection result = Stream.concat(collection1.stream(), collection2.stream()).collect(Collectors.toList()); assertTrue(result.equals(Arrays.asList("Dog", "Cat", "Bird", "Cow", "Moose"))); } @@ -36,10 +33,7 @@ public class JoinSplitCollectionsUnitTest { public void whenJoiningTwoCollectionsWithFilter_thenJoined() { Collection collection1 = Arrays.asList("Dog", "Cat"); Collection collection2 = Arrays.asList("Bird", "Cow", "Moose"); - Collection result = Stream.concat( - collection1.stream(), collection2.stream()) - .filter(e -> e.length() == 3) - .collect(Collectors.toList()); + Collection result = Stream.concat(collection1.stream(), collection2.stream()).filter(e -> e.length() == 3).collect(Collectors.toList()); assertTrue(result.equals(Arrays.asList("Dog", "Cat", "Cow"))); } @@ -67,9 +61,7 @@ public class JoinSplitCollectionsUnitTest { animals.put(2, "Cat"); animals.put(3, "Cow"); - String result = animals.entrySet().stream() - .map(entry -> entry.getKey() + " = " + entry.getValue()) - .collect(Collectors.joining(", ")); + String result = animals.entrySet().stream().map(entry -> entry.getKey() + " = " + entry.getValue()).collect(Collectors.joining(", ")); assertEquals(result, "1 = Dog, 2 = Cat, 3 = Cow"); } @@ -80,10 +72,7 @@ public class JoinSplitCollectionsUnitTest { nested.add(Arrays.asList("Dog", "Cat")); nested.add(Arrays.asList("Cow", "Pig")); - String result = nested.stream().map( - nextList -> nextList.stream() - .collect(Collectors.joining("-"))) - .collect(Collectors.joining("; ")); + String result = nested.stream().map(nextList -> nextList.stream().collect(Collectors.joining("-"))).collect(Collectors.joining("; ")); assertEquals(result, "Dog-Cat; Cow-Pig"); } @@ -91,17 +80,14 @@ public class JoinSplitCollectionsUnitTest { @Test public void whenConvertCollectionToStringAndSkipNull_thenConverted() { Collection animals = Arrays.asList("Dog", "Cat", null, "Moose"); - String result = animals.stream() - .filter(Objects::nonNull) - .collect(Collectors.joining(", ")); + String result = animals.stream().filter(Objects::nonNull).collect(Collectors.joining(", ")); assertEquals(result, "Dog, Cat, Moose"); } @Test public void whenSplitCollectionHalf_thenConverted() { - Collection animals = Arrays.asList( - "Dog", "Cat", "Cow", "Bird", "Moose", "Pig"); + Collection animals = Arrays.asList("Dog", "Cat", "Cow", "Bird", "Moose", "Pig"); Collection result1 = new ArrayList<>(); Collection result2 = new ArrayList<>(); AtomicInteger count = new AtomicInteger(); @@ -122,9 +108,8 @@ public class JoinSplitCollectionsUnitTest { @Test public void whenSplitArrayByWordLength_thenConverted() { - String[] animals = new String[] { "Dog", "Cat", "Bird", "Cow", "Pig", "Moose"}; - Map> result = Arrays.stream(animals) - .collect(Collectors.groupingBy(String::length)); + String[] animals = new String[] { "Dog", "Cat", "Bird", "Cow", "Pig", "Moose" }; + Map> result = Arrays.stream(animals).collect(Collectors.groupingBy(String::length)); assertTrue(result.get(3).equals(Arrays.asList("Dog", "Cat", "Cow", "Pig"))); assertTrue(result.get(4).equals(Arrays.asList("Bird"))); @@ -151,9 +136,7 @@ public class JoinSplitCollectionsUnitTest { public void whenConvertStringToMap_thenConverted() { String animals = "1 = Dog, 2 = Cat, 3 = Bird"; - Map result = Arrays.stream( - animals.split(", ")).map(next -> next.split(" = ")) - .collect(Collectors.toMap(entry -> Integer.parseInt(entry[0]), entry -> entry[1])); + Map result = Arrays.stream(animals.split(", ")).map(next -> next.split(" = ")).collect(Collectors.toMap(entry -> Integer.parseInt(entry[0]), entry -> entry[1])); assertEquals(result.get(1), "Dog"); assertEquals(result.get(2), "Cat"); @@ -164,10 +147,7 @@ public class JoinSplitCollectionsUnitTest { public void whenConvertCollectionToStringMultipleSeparators_thenConverted() { String animals = "Dog. , Cat, Bird. Cow"; - Collection result = Arrays.stream(animals.split("[,|.]")) - .map(String::trim) - .filter(next -> !next.isEmpty()) - .collect(Collectors.toList()); + Collection result = Arrays.stream(animals.split("[,|.]")).map(String::trim).filter(next -> !next.isEmpty()).collect(Collectors.toList()); assertTrue(result.equals(Arrays.asList("Dog", "Cat", "Bird", "Cow"))); }