Merge remote-tracking branch 'origin/master'
# Conflicts: # core-java/src/main/java/com/baeldung/generics/Generics.java # core-java/src/test/java/com/baeldung/generics/GenericsTest.java
This commit is contained in:
commit
aba015e1ad
@ -13,12 +13,11 @@ public class Generics {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// definition of a generic method
|
// definition of a generic method
|
||||||
public static <T, G> List<G> fromArrayToList(T[] a, Function<T, G> mapperFunction) {
|
public static <T, G> List<G> fromArrayToList(T[] a, List<G> list, Function<T, G> mapperFunction) {
|
||||||
return Arrays.stream(a).map(mapperFunction)
|
List<T> listWithTypeT = Arrays.stream(a).collect(Collectors.toList());
|
||||||
.collect(Collectors.toList());
|
return listWithTypeT.stream().map(mapperFunction).collect(Collectors.toList());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// example of a generic method that has Number as an upper bound for T
|
// example of a generic method that has Number as an upper bound for T
|
||||||
public static <T extends Number> List<T> fromArrayToListWithUpperBound(T[] a) {
|
public static <T extends Number> List<T> fromArrayToListWithUpperBound(T[] a) {
|
||||||
return Arrays.stream(a).collect(Collectors.toList());
|
return Arrays.stream(a).collect(Collectors.toList());
|
||||||
|
@ -14,7 +14,7 @@ public class GenericsTest {
|
|||||||
// testing the generic method with Integer
|
// testing the generic method with Integer
|
||||||
@Test
|
@Test
|
||||||
public void givenArrayOfIntegers_thanListOfIntegersReturnedOK() {
|
public void givenArrayOfIntegers_thanListOfIntegersReturnedOK() {
|
||||||
Integer[] intArray = {1, 2, 3, 4, 5};
|
Integer[] intArray = { 1, 2, 3, 4, 5 };
|
||||||
List<Integer> list = Generics.fromArrayToList(intArray);
|
List<Integer> list = Generics.fromArrayToList(intArray);
|
||||||
|
|
||||||
assertThat(list, hasItems(intArray));
|
assertThat(list, hasItems(intArray));
|
||||||
@ -23,15 +23,16 @@ public class GenericsTest {
|
|||||||
// testing the generic method with Integer and String type
|
// testing the generic method with Integer and String type
|
||||||
@Test
|
@Test
|
||||||
public void givenArrayOfIntegers_thanListOfStringReturnedOK() {
|
public void givenArrayOfIntegers_thanListOfStringReturnedOK() {
|
||||||
Integer[] intArray = {1, 2, 3, 4, 5};
|
Integer[] intArray = { 1, 2, 3, 4, 5 };
|
||||||
List<String> stringList = Generics.fromArrayToList(intArray, Object::toString);
|
List<String> stringList = new ArrayList<>();
|
||||||
|
stringList = Generics.fromArrayToList(intArray, stringList, Object::toString);
|
||||||
assertThat(stringList, hasItems("1", "2", "3", "4", "5"));
|
assertThat(stringList, hasItems("1", "2", "3", "4", "5"));
|
||||||
}
|
}
|
||||||
|
|
||||||
// testing the generic method with String
|
// testing the generic method with String
|
||||||
@Test
|
@Test
|
||||||
public void givenArrayOfStrings_thanListOfStringsReturnedOK() {
|
public void givenArrayOfStrings_thanListOfStringsReturnedOK() {
|
||||||
String[] stringArray = {"hello1", "hello2", "hello3", "hello4", "hello5"};
|
String[] stringArray = { "hello1", "hello2", "hello3", "hello4", "hello5" };
|
||||||
List<String> list = Generics.fromArrayToList(stringArray);
|
List<String> list = Generics.fromArrayToList(stringArray);
|
||||||
|
|
||||||
assertThat(list, hasItems(stringArray));
|
assertThat(list, hasItems(stringArray));
|
||||||
@ -42,7 +43,7 @@ public class GenericsTest {
|
|||||||
// extend Number it will fail to compile
|
// extend Number it will fail to compile
|
||||||
@Test
|
@Test
|
||||||
public void givenArrayOfIntegersAndNumberUpperBound_thanListOfIntegersReturnedOK() {
|
public void givenArrayOfIntegersAndNumberUpperBound_thanListOfIntegersReturnedOK() {
|
||||||
Integer[] intArray = {1, 2, 3, 4, 5};
|
Integer[] intArray = { 1, 2, 3, 4, 5 };
|
||||||
List<Integer> list = Generics.fromArrayToListWithUpperBound(intArray);
|
List<Integer> list = Generics.fromArrayToListWithUpperBound(intArray);
|
||||||
|
|
||||||
assertThat(list, hasItems(intArray));
|
assertThat(list, hasItems(intArray));
|
||||||
|
@ -34,7 +34,7 @@ public class AsyncEchoServer {
|
|||||||
ByteBuffer buffer = ByteBuffer.allocate(32);
|
ByteBuffer buffer = ByteBuffer.allocate(32);
|
||||||
Future<Integer> readResult = clientChannel.read(buffer);
|
Future<Integer> readResult = clientChannel.read(buffer);
|
||||||
|
|
||||||
//do some computation
|
// do some computation
|
||||||
|
|
||||||
readResult.get();
|
readResult.get();
|
||||||
|
|
||||||
@ -46,7 +46,7 @@ public class AsyncEchoServer {
|
|||||||
buffer = ByteBuffer.wrap(new String(message).getBytes());
|
buffer = ByteBuffer.wrap(new String(message).getBytes());
|
||||||
Future<Integer> writeResult = clientChannel.write(buffer);
|
Future<Integer> writeResult = clientChannel.write(buffer);
|
||||||
|
|
||||||
//do some computation
|
// do some computation
|
||||||
writeResult.get();
|
writeResult.get();
|
||||||
buffer.clear();
|
buffer.clear();
|
||||||
|
|
||||||
|
@ -82,7 +82,6 @@ public class AsyncEchoServer2 {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
new AsyncEchoServer2();
|
new AsyncEchoServer2();
|
||||||
}
|
}
|
||||||
|
@ -1,14 +1,17 @@
|
|||||||
package com.baeldung.java8.optional;
|
package com.baeldung.java8.optional;
|
||||||
|
|
||||||
import com.baeldung.java_8_features.Person;
|
import static org.junit.Assert.assertEquals;
|
||||||
import org.junit.Test;
|
import static org.junit.Assert.assertFalse;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.NoSuchElementException;
|
import java.util.NoSuchElementException;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
import static org.junit.Assert.*;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import com.baeldung.java_8_features.Person;
|
||||||
|
|
||||||
public class OptionalTest {
|
public class OptionalTest {
|
||||||
// creating Optional
|
// creating Optional
|
||||||
@ -87,13 +90,9 @@ public class OptionalTest {
|
|||||||
public void whenOptionalFilterWorks_thenCorrect() {
|
public void whenOptionalFilterWorks_thenCorrect() {
|
||||||
Integer year = 2016;
|
Integer year = 2016;
|
||||||
Optional<Integer> yearOptional = Optional.of(year);
|
Optional<Integer> yearOptional = Optional.of(year);
|
||||||
boolean is2016 = yearOptional
|
boolean is2016 = yearOptional.filter(y -> y == 2016).isPresent();
|
||||||
.filter(y -> y == 2016)
|
|
||||||
.isPresent();
|
|
||||||
assertTrue(is2016);
|
assertTrue(is2016);
|
||||||
boolean is2017 = yearOptional
|
boolean is2017 = yearOptional.filter(y -> y == 2017).isPresent();
|
||||||
.filter(y -> y == 2017)
|
|
||||||
.isPresent();
|
|
||||||
assertFalse(is2017);
|
assertFalse(is2017);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -103,8 +102,7 @@ public class OptionalTest {
|
|||||||
List<String> companyNames = Arrays.asList("paypal", "oracle", "", "microsoft", "", "apple");
|
List<String> companyNames = Arrays.asList("paypal", "oracle", "", "microsoft", "", "apple");
|
||||||
Optional<List<String>> listOptional = Optional.of(companyNames);
|
Optional<List<String>> listOptional = Optional.of(companyNames);
|
||||||
|
|
||||||
int size = listOptional.map(List::size)
|
int size = listOptional.map(List::size).orElse(0);
|
||||||
.orElse(0);
|
|
||||||
assertEquals(6, size);
|
assertEquals(6, size);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -113,9 +111,7 @@ public class OptionalTest {
|
|||||||
String name = "baeldung";
|
String name = "baeldung";
|
||||||
Optional<String> nameOptional = Optional.of(name);
|
Optional<String> nameOptional = Optional.of(name);
|
||||||
|
|
||||||
int len = nameOptional
|
int len = nameOptional.map(String::length).orElse(0);
|
||||||
.map(String::length)
|
|
||||||
.orElse(0);
|
|
||||||
assertEquals(8, len);
|
assertEquals(8, len);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -123,14 +119,10 @@ public class OptionalTest {
|
|||||||
public void givenOptional_whenMapWorksWithFilter_thenCorrect() {
|
public void givenOptional_whenMapWorksWithFilter_thenCorrect() {
|
||||||
String password = " password ";
|
String password = " password ";
|
||||||
Optional<String> passOpt = Optional.of(password);
|
Optional<String> passOpt = Optional.of(password);
|
||||||
boolean correctPassword = passOpt
|
boolean correctPassword = passOpt.filter(pass -> pass.equals("password")).isPresent();
|
||||||
.filter(pass -> pass.equals("password"))
|
|
||||||
.isPresent();
|
|
||||||
assertFalse(correctPassword);
|
assertFalse(correctPassword);
|
||||||
|
|
||||||
correctPassword = passOpt.map(String::trim)
|
correctPassword = passOpt.map(String::trim).filter(pass -> pass.equals("password")).isPresent();
|
||||||
.filter(pass -> pass.equals("password"))
|
|
||||||
.isPresent();
|
|
||||||
assertTrue(correctPassword);
|
assertTrue(correctPassword);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -140,17 +132,12 @@ public class OptionalTest {
|
|||||||
Person person = new Person("john", 26);
|
Person person = new Person("john", 26);
|
||||||
Optional<Person> personOptional = Optional.of(person);
|
Optional<Person> personOptional = Optional.of(person);
|
||||||
|
|
||||||
Optional<Optional<String>> nameOptionalWrapper = personOptional
|
Optional<Optional<String>> nameOptionalWrapper = personOptional.map(Person::getName);
|
||||||
.map(Person::getName);
|
Optional<String> nameOptional = nameOptionalWrapper.orElseThrow(IllegalArgumentException::new);
|
||||||
Optional<String> nameOptional = nameOptionalWrapper
|
String name1 = nameOptional.orElseThrow(IllegalArgumentException::new);
|
||||||
.orElseThrow(IllegalArgumentException::new);
|
|
||||||
String name1 = nameOptional
|
|
||||||
.orElseThrow(IllegalArgumentException::new);
|
|
||||||
assertEquals("john", name1);
|
assertEquals("john", name1);
|
||||||
|
|
||||||
String name = personOptional
|
String name = personOptional.flatMap(Person::getName).orElseThrow(IllegalArgumentException::new);
|
||||||
.flatMap(Person::getName)
|
|
||||||
.orElseThrow(IllegalArgumentException::new);
|
|
||||||
assertEquals("john", name);
|
assertEquals("john", name);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -160,10 +147,7 @@ public class OptionalTest {
|
|||||||
person.setPassword("password");
|
person.setPassword("password");
|
||||||
Optional<Person> personOptional = Optional.of(person);
|
Optional<Person> personOptional = Optional.of(person);
|
||||||
|
|
||||||
String password = personOptional
|
String password = personOptional.flatMap(Person::getPassword).filter(cleanPass -> cleanPass.equals("password")).orElseThrow(IllegalArgumentException::new);
|
||||||
.flatMap(Person::getPassword)
|
|
||||||
.filter(cleanPass -> cleanPass.equals("password"))
|
|
||||||
.orElseThrow(IllegalArgumentException::new);
|
|
||||||
assertEquals("password", password);
|
assertEquals("password", password);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -171,8 +155,7 @@ public class OptionalTest {
|
|||||||
@Test
|
@Test
|
||||||
public void whenOrElseWorks_thenCorrect() {
|
public void whenOrElseWorks_thenCorrect() {
|
||||||
String nullName = null;
|
String nullName = null;
|
||||||
String name = Optional.ofNullable(nullName)
|
String name = Optional.ofNullable(nullName).orElse("john");
|
||||||
.orElse("john");
|
|
||||||
assertEquals("john", name);
|
assertEquals("john", name);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -180,8 +163,7 @@ public class OptionalTest {
|
|||||||
@Test
|
@Test
|
||||||
public void whenOrElseGetWorks_thenCorrect() {
|
public void whenOrElseGetWorks_thenCorrect() {
|
||||||
String nullName = null;
|
String nullName = null;
|
||||||
String name = Optional.ofNullable(nullName)
|
String name = Optional.ofNullable(nullName).orElseGet(() -> "john");
|
||||||
.orElseGet(() -> "john");
|
|
||||||
assertEquals("john", name);
|
assertEquals("john", name);
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -190,13 +172,11 @@ public class OptionalTest {
|
|||||||
public void whenOrElseGetAndOrElseOverlap_thenCorrect() {
|
public void whenOrElseGetAndOrElseOverlap_thenCorrect() {
|
||||||
String text = null;
|
String text = null;
|
||||||
System.out.println("Using orElseGet:");
|
System.out.println("Using orElseGet:");
|
||||||
String defaultText = Optional.ofNullable(text)
|
String defaultText = Optional.ofNullable(text).orElseGet(this::getMyDefault);
|
||||||
.orElseGet(this::getMyDefault);
|
|
||||||
assertEquals("Default Value", defaultText);
|
assertEquals("Default Value", defaultText);
|
||||||
|
|
||||||
System.out.println("Using orElse:");
|
System.out.println("Using orElse:");
|
||||||
defaultText = Optional.ofNullable(text)
|
defaultText = Optional.ofNullable(text).orElse(getMyDefault());
|
||||||
.orElse(getMyDefault());
|
|
||||||
assertEquals("Default Value", defaultText);
|
assertEquals("Default Value", defaultText);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -204,13 +184,11 @@ public class OptionalTest {
|
|||||||
public void whenOrElseGetAndOrElseDiffer_thenCorrect() {
|
public void whenOrElseGetAndOrElseDiffer_thenCorrect() {
|
||||||
String text = "Text present";
|
String text = "Text present";
|
||||||
System.out.println("Using orElseGet:");
|
System.out.println("Using orElseGet:");
|
||||||
String defaultText = Optional.ofNullable(text)
|
String defaultText = Optional.ofNullable(text).orElseGet(this::getMyDefault);
|
||||||
.orElseGet(this::getMyDefault);
|
|
||||||
assertEquals("Text present", defaultText);
|
assertEquals("Text present", defaultText);
|
||||||
|
|
||||||
System.out.println("Using orElse:");
|
System.out.println("Using orElse:");
|
||||||
defaultText = Optional.ofNullable(text)
|
defaultText = Optional.ofNullable(text).orElse(getMyDefault());
|
||||||
.orElse(getMyDefault());
|
|
||||||
assertEquals("Text present", defaultText);
|
assertEquals("Text present", defaultText);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -218,8 +196,7 @@ public class OptionalTest {
|
|||||||
@Test(expected = IllegalArgumentException.class)
|
@Test(expected = IllegalArgumentException.class)
|
||||||
public void whenOrElseThrowWorks_thenCorrect() {
|
public void whenOrElseThrowWorks_thenCorrect() {
|
||||||
String nullName = null;
|
String nullName = null;
|
||||||
String name = Optional.ofNullable(nullName)
|
String name = Optional.ofNullable(nullName).orElseThrow(IllegalArgumentException::new);
|
||||||
.orElseThrow(IllegalArgumentException::new);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getMyDefault() {
|
public String getMyDefault() {
|
||||||
|
@ -13,20 +13,18 @@ public class JoinSplitCollectionsUnitTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenJoiningTwoArrays_thenJoined() {
|
public void whenJoiningTwoArrays_thenJoined() {
|
||||||
String[] animals1 = new String[]{"Dog", "Cat"};
|
String[] animals1 = new String[] { "Dog", "Cat" };
|
||||||
String[] animals2 = new String[]{"Bird", "Cow"};
|
String[] animals2 = new String[] { "Bird", "Cow" };
|
||||||
String[] result = Stream.concat(Arrays.stream(animals1), Arrays.stream(animals2))
|
String[] result = Stream.concat(Arrays.stream(animals1), Arrays.stream(animals2)).toArray(String[]::new);
|
||||||
.toArray(String[]::new);
|
|
||||||
|
|
||||||
assertArrayEquals(result, new String[]{"Dog", "Cat", "Bird", "Cow"});
|
assertArrayEquals(result, new String[] { "Dog", "Cat", "Bird", "Cow" });
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenJoiningTwoCollections_thenJoined() {
|
public void whenJoiningTwoCollections_thenJoined() {
|
||||||
Collection<Integer> collection1 = Arrays.asList(7, 8, 9);
|
Collection<Integer> collection1 = Arrays.asList(7, 8, 9);
|
||||||
Collection<Integer> collection2 = Arrays.asList(10, 11, 12);
|
Collection<Integer> collection2 = Arrays.asList(10, 11, 12);
|
||||||
Collection<Integer> result = Stream.concat(collection1.stream(), collection2.stream())
|
Collection<Integer> result = Stream.concat(collection1.stream(), collection2.stream()).collect(Collectors.toList());
|
||||||
.collect(Collectors.toList());
|
|
||||||
|
|
||||||
assertTrue(result.equals(Arrays.asList(7, 8, 9, 10, 11, 12)));
|
assertTrue(result.equals(Arrays.asList(7, 8, 9, 10, 11, 12)));
|
||||||
}
|
}
|
||||||
@ -35,18 +33,15 @@ public class JoinSplitCollectionsUnitTest {
|
|||||||
public void whenJoiningTwoCollectionsWithFilter_thenJoined() {
|
public void whenJoiningTwoCollectionsWithFilter_thenJoined() {
|
||||||
Collection<Integer> collection1 = Arrays.asList(7, 8, 11);
|
Collection<Integer> collection1 = Arrays.asList(7, 8, 11);
|
||||||
Collection<Integer> collection2 = Arrays.asList(9, 12, 10);
|
Collection<Integer> collection2 = Arrays.asList(9, 12, 10);
|
||||||
Collection<Integer> result = Stream.concat(collection1.stream(), collection2.stream())
|
Collection<Integer> result = Stream.concat(collection1.stream(), collection2.stream()).filter(next -> next <= 10).collect(Collectors.toList());
|
||||||
.filter(next -> next <= 10)
|
|
||||||
.collect(Collectors.toList());
|
|
||||||
|
|
||||||
assertTrue(result.equals(Arrays.asList(7, 8, 9, 10)));
|
assertTrue(result.equals(Arrays.asList(7, 8, 9, 10)));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenConvertArrayToString_thenConverted() {
|
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)
|
String result = Arrays.stream(colors).collect(Collectors.joining(", "));
|
||||||
.collect(Collectors.joining(", "));
|
|
||||||
|
|
||||||
assertEquals(result, "Red, Blue, Green, Yellow");
|
assertEquals(result, "Red, Blue, Green, Yellow");
|
||||||
}
|
}
|
||||||
@ -54,8 +49,7 @@ public class JoinSplitCollectionsUnitTest {
|
|||||||
@Test
|
@Test
|
||||||
public void whenConvertCollectionToString_thenConverted() {
|
public void whenConvertCollectionToString_thenConverted() {
|
||||||
Collection<String> directions = Arrays.asList("Left", "Right", "Top", "Bottom");
|
Collection<String> directions = Arrays.asList("Left", "Right", "Top", "Bottom");
|
||||||
String result = directions.stream()
|
String result = directions.stream().collect(Collectors.joining(", "));
|
||||||
.collect(Collectors.joining(", "));
|
|
||||||
|
|
||||||
assertEquals(result, "Left, Right, Top, Bottom");
|
assertEquals(result, "Left, Right, Top, Bottom");
|
||||||
}
|
}
|
||||||
@ -67,9 +61,7 @@ public class JoinSplitCollectionsUnitTest {
|
|||||||
users.put(2, "Paul Smith");
|
users.put(2, "Paul Smith");
|
||||||
users.put(3, "Susan Anderson");
|
users.put(3, "Susan Anderson");
|
||||||
|
|
||||||
String result = users.entrySet().stream()
|
String result = users.entrySet().stream().map(entry -> entry.getKey() + " = " + entry.getValue()).collect(Collectors.joining(", "));
|
||||||
.map(entry -> entry.getKey() + " = " + entry.getValue())
|
|
||||||
.collect(Collectors.joining(", "));
|
|
||||||
|
|
||||||
assertEquals(result, "1 = John Doe, 2 = Paul Smith, 3 = Susan Anderson");
|
assertEquals(result, "1 = John Doe, 2 = Paul Smith, 3 = Susan Anderson");
|
||||||
}
|
}
|
||||||
@ -80,10 +72,7 @@ public class JoinSplitCollectionsUnitTest {
|
|||||||
nested.add(Arrays.asList("Left", "Right", "Top", "Bottom"));
|
nested.add(Arrays.asList("Left", "Right", "Top", "Bottom"));
|
||||||
nested.add(Arrays.asList("Red", "Blue", "Green", "Yellow"));
|
nested.add(Arrays.asList("Red", "Blue", "Green", "Yellow"));
|
||||||
|
|
||||||
String result = nested.stream()
|
String result = nested.stream().map(nextList -> nextList.stream().collect(Collectors.joining("-"))).collect(Collectors.joining("; "));
|
||||||
.map(nextList -> nextList.stream()
|
|
||||||
.collect(Collectors.joining("-")))
|
|
||||||
.collect(Collectors.joining("; "));
|
|
||||||
|
|
||||||
assertEquals(result, "Left-Right-Top-Bottom; Red-Blue-Green-Yellow");
|
assertEquals(result, "Left-Right-Top-Bottom; Red-Blue-Green-Yellow");
|
||||||
}
|
}
|
||||||
@ -91,9 +80,7 @@ public class JoinSplitCollectionsUnitTest {
|
|||||||
@Test
|
@Test
|
||||||
public void whenConvertCollectionToStringAndSkipNull_thenConverted() {
|
public void whenConvertCollectionToStringAndSkipNull_thenConverted() {
|
||||||
Collection<String> fruits = Arrays.asList("Apple", "Orange", null, "Grape");
|
Collection<String> fruits = Arrays.asList("Apple", "Orange", null, "Grape");
|
||||||
String result = fruits.stream()
|
String result = fruits.stream().filter(Objects::nonNull).collect(Collectors.joining(", "));
|
||||||
.filter(Objects::nonNull)
|
|
||||||
.collect(Collectors.joining(", "));
|
|
||||||
|
|
||||||
assertEquals(result, "Apple, Orange, Grape");
|
assertEquals(result, "Apple, Orange, Grape");
|
||||||
}
|
}
|
||||||
@ -121,9 +108,8 @@ public class JoinSplitCollectionsUnitTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenSplitArrayByWordLength_thenConverted() {
|
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)
|
Map<Integer, List<String>> result = Arrays.stream(words).collect(Collectors.groupingBy(String::length));
|
||||||
.collect(Collectors.groupingBy(String::length));
|
|
||||||
|
|
||||||
assertTrue(result.get(2).equals(Arrays.asList("it", "my")));
|
assertTrue(result.get(2).equals(Arrays.asList("it", "my")));
|
||||||
assertTrue(result.get(3).equals(Arrays.asList("bye", "and")));
|
assertTrue(result.get(3).equals(Arrays.asList("bye", "and")));
|
||||||
@ -135,7 +121,7 @@ public class JoinSplitCollectionsUnitTest {
|
|||||||
String colors = "Red, Blue, Green, Yellow";
|
String colors = "Red, Blue, Green, Yellow";
|
||||||
String[] result = colors.split(", ");
|
String[] result = colors.split(", ");
|
||||||
|
|
||||||
assertArrayEquals(result, new String[]{"Red", "Blue", "Green", "Yellow"});
|
assertArrayEquals(result, new String[] { "Red", "Blue", "Green", "Yellow" });
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -150,9 +136,7 @@ public class JoinSplitCollectionsUnitTest {
|
|||||||
public void whenConvertStringToMap_thenConverted() {
|
public void whenConvertStringToMap_thenConverted() {
|
||||||
String users = "1 = John Doe, 2 = Paul Smith, 3 = Susan Anderson";
|
String users = "1 = John Doe, 2 = Paul Smith, 3 = Susan Anderson";
|
||||||
|
|
||||||
Map<Integer, String> result = Arrays.stream(users.split(", "))
|
Map<Integer, String> result = Arrays.stream(users.split(", ")).map(next -> next.split(" = ")).collect(Collectors.toMap(entry -> Integer.parseInt(entry[0]), entry -> entry[1]));
|
||||||
.map(next -> next.split(" = "))
|
|
||||||
.collect(Collectors.toMap(entry -> Integer.parseInt(entry[0]), entry -> entry[1]));
|
|
||||||
|
|
||||||
assertEquals(result.get(1), "John Doe");
|
assertEquals(result.get(1), "John Doe");
|
||||||
assertEquals(result.get(2), "Paul Smith");
|
assertEquals(result.get(2), "Paul Smith");
|
||||||
@ -163,10 +147,7 @@ public class JoinSplitCollectionsUnitTest {
|
|||||||
public void whenConvertCollectionToStringMultipleSeparators_thenConverted() {
|
public void whenConvertCollectionToStringMultipleSeparators_thenConverted() {
|
||||||
String fruits = "Apple. , Orange, Grape. Lemon";
|
String fruits = "Apple. , Orange, Grape. Lemon";
|
||||||
|
|
||||||
Collection<String> result = Arrays.stream(fruits.split("[,|.]"))
|
Collection<String> result = Arrays.stream(fruits.split("[,|.]")).map(String::trim).filter(next -> !next.isEmpty()).collect(Collectors.toList());
|
||||||
.map(String::trim)
|
|
||||||
.filter(next -> !next.isEmpty())
|
|
||||||
.collect(Collectors.toList());
|
|
||||||
|
|
||||||
assertTrue(result.equals(Arrays.asList("Apple", "Orange", "Grape", "Lemon")));
|
assertTrue(result.equals(Arrays.asList("Apple", "Orange", "Grape", "Lemon")));
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user