Refactor JoinSplitCollectionsUnitTest
This commit is contained in:
parent
34e5a6475f
commit
a6bdad2cb9
|
@ -1,31 +1,24 @@
|
|||
package org.baeldung.java.collections;
|
||||
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class JoinSplitCollectionsUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenJoiningTwoArrays_thenJoined() {
|
||||
String[] animals1 = new String[] { "Dog", "Cat" };
|
||||
String[] animals2 = new String[] { "Bird", "Cow" };
|
||||
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);
|
||||
|
||||
assertArrayEquals(result, new String[] { "Dog", "Cat", "Bird", "Cow" });
|
||||
assertArrayEquals(result, new String[]{"Dog", "Cat", "Bird", "Cow"});
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -51,7 +44,7 @@ public class JoinSplitCollectionsUnitTest {
|
|||
|
||||
@Test
|
||||
public void whenConvertArrayToString_thenConverted() {
|
||||
String[] colors = new String[] { "Red", "Blue", "Green", "Yellow" };
|
||||
String[] colors = new String[]{"Red", "Blue", "Green", "Yellow"};
|
||||
String result = Arrays.stream(colors)
|
||||
.collect(Collectors.joining(", "));
|
||||
|
||||
|
@ -99,7 +92,7 @@ public class JoinSplitCollectionsUnitTest {
|
|||
public void whenConvertCollectionToStringAndSkipNull_thenConverted() {
|
||||
Collection<String> fruits = Arrays.asList("Apple", "Orange", null, "Grape");
|
||||
String result = fruits.stream()
|
||||
.filter(next -> next != null)
|
||||
.filter(Objects::nonNull)
|
||||
.collect(Collectors.joining(", "));
|
||||
|
||||
assertEquals(result, "Apple, Orange, Grape");
|
||||
|
@ -128,9 +121,9 @@ public class JoinSplitCollectionsUnitTest {
|
|||
|
||||
@Test
|
||||
public void whenSplitArrayByWordLength_thenConverted() {
|
||||
String[] words = new String[] { "bye", "cold", "it", "and", "my", "word" };
|
||||
String[] words = new String[]{"bye", "cold", "it", "and", "my", "word"};
|
||||
Map<Integer, List<String>> result = Arrays.stream(words)
|
||||
.collect(Collectors.groupingBy(word -> word.length()));
|
||||
.collect(Collectors.groupingBy(String::length));
|
||||
|
||||
assertTrue(result.get(2).equals(Arrays.asList("it", "my")));
|
||||
assertTrue(result.get(3).equals(Arrays.asList("bye", "and")));
|
||||
|
@ -142,7 +135,7 @@ public class JoinSplitCollectionsUnitTest {
|
|||
String colors = "Red, Blue, Green, Yellow";
|
||||
String[] result = colors.split(", ");
|
||||
|
||||
assertArrayEquals(result, new String[] { "Red", "Blue", "Green", "Yellow" });
|
||||
assertArrayEquals(result, new String[]{"Red", "Blue", "Green", "Yellow"});
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
Loading…
Reference in New Issue