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
|
||||
public static <T, G> List<G> fromArrayToList(T[] a, Function<T, G> mapperFunction) {
|
||||
return Arrays.stream(a).map(mapperFunction)
|
||||
.collect(Collectors.toList());
|
||||
public static <T, G> List<G> fromArrayToList(T[] a, List<G> list, Function<T, G> mapperFunction) {
|
||||
List<T> listWithTypeT = Arrays.stream(a).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
|
||||
public static <T extends Number> List<T> fromArrayToListWithUpperBound(T[] a) {
|
||||
return Arrays.stream(a).collect(Collectors.toList());
|
||||
|
|
|
@ -24,7 +24,8 @@ public class GenericsTest {
|
|||
@Test
|
||||
public void givenArrayOfIntegers_thanListOfStringReturnedOK() {
|
||||
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"));
|
||||
}
|
||||
|
||||
|
|
|
@ -82,7 +82,6 @@ public class AsyncEchoServer2 {
|
|||
|
||||
}
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
new AsyncEchoServer2();
|
||||
}
|
||||
|
|
|
@ -1,14 +1,17 @@
|
|||
package com.baeldung.java8.optional;
|
||||
|
||||
import com.baeldung.java_8_features.Person;
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.NoSuchElementException;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.java_8_features.Person;
|
||||
|
||||
public class OptionalTest {
|
||||
// creating Optional
|
||||
|
@ -87,13 +90,9 @@ public class OptionalTest {
|
|||
public void whenOptionalFilterWorks_thenCorrect() {
|
||||
Integer year = 2016;
|
||||
Optional<Integer> yearOptional = Optional.of(year);
|
||||
boolean is2016 = yearOptional
|
||||
.filter(y -> y == 2016)
|
||||
.isPresent();
|
||||
boolean is2016 = yearOptional.filter(y -> y == 2016).isPresent();
|
||||
assertTrue(is2016);
|
||||
boolean is2017 = yearOptional
|
||||
.filter(y -> y == 2017)
|
||||
.isPresent();
|
||||
boolean is2017 = yearOptional.filter(y -> y == 2017).isPresent();
|
||||
assertFalse(is2017);
|
||||
}
|
||||
|
||||
|
@ -103,8 +102,7 @@ public class OptionalTest {
|
|||
List<String> companyNames = Arrays.asList("paypal", "oracle", "", "microsoft", "", "apple");
|
||||
Optional<List<String>> listOptional = Optional.of(companyNames);
|
||||
|
||||
int size = listOptional.map(List::size)
|
||||
.orElse(0);
|
||||
int size = listOptional.map(List::size).orElse(0);
|
||||
assertEquals(6, size);
|
||||
}
|
||||
|
||||
|
@ -113,9 +111,7 @@ public class OptionalTest {
|
|||
String name = "baeldung";
|
||||
Optional<String> nameOptional = Optional.of(name);
|
||||
|
||||
int len = nameOptional
|
||||
.map(String::length)
|
||||
.orElse(0);
|
||||
int len = nameOptional.map(String::length).orElse(0);
|
||||
assertEquals(8, len);
|
||||
}
|
||||
|
||||
|
@ -123,14 +119,10 @@ public class OptionalTest {
|
|||
public void givenOptional_whenMapWorksWithFilter_thenCorrect() {
|
||||
String password = " password ";
|
||||
Optional<String> passOpt = Optional.of(password);
|
||||
boolean correctPassword = passOpt
|
||||
.filter(pass -> pass.equals("password"))
|
||||
.isPresent();
|
||||
boolean correctPassword = passOpt.filter(pass -> pass.equals("password")).isPresent();
|
||||
assertFalse(correctPassword);
|
||||
|
||||
correctPassword = passOpt.map(String::trim)
|
||||
.filter(pass -> pass.equals("password"))
|
||||
.isPresent();
|
||||
correctPassword = passOpt.map(String::trim).filter(pass -> pass.equals("password")).isPresent();
|
||||
assertTrue(correctPassword);
|
||||
}
|
||||
|
||||
|
@ -140,17 +132,12 @@ public class OptionalTest {
|
|||
Person person = new Person("john", 26);
|
||||
Optional<Person> personOptional = Optional.of(person);
|
||||
|
||||
Optional<Optional<String>> nameOptionalWrapper = personOptional
|
||||
.map(Person::getName);
|
||||
Optional<String> nameOptional = nameOptionalWrapper
|
||||
.orElseThrow(IllegalArgumentException::new);
|
||||
String name1 = nameOptional
|
||||
.orElseThrow(IllegalArgumentException::new);
|
||||
Optional<Optional<String>> nameOptionalWrapper = personOptional.map(Person::getName);
|
||||
Optional<String> nameOptional = nameOptionalWrapper.orElseThrow(IllegalArgumentException::new);
|
||||
String name1 = nameOptional.orElseThrow(IllegalArgumentException::new);
|
||||
assertEquals("john", name1);
|
||||
|
||||
String name = personOptional
|
||||
.flatMap(Person::getName)
|
||||
.orElseThrow(IllegalArgumentException::new);
|
||||
String name = personOptional.flatMap(Person::getName).orElseThrow(IllegalArgumentException::new);
|
||||
assertEquals("john", name);
|
||||
}
|
||||
|
||||
|
@ -160,10 +147,7 @@ public class OptionalTest {
|
|||
person.setPassword("password");
|
||||
Optional<Person> personOptional = Optional.of(person);
|
||||
|
||||
String password = personOptional
|
||||
.flatMap(Person::getPassword)
|
||||
.filter(cleanPass -> cleanPass.equals("password"))
|
||||
.orElseThrow(IllegalArgumentException::new);
|
||||
String password = personOptional.flatMap(Person::getPassword).filter(cleanPass -> cleanPass.equals("password")).orElseThrow(IllegalArgumentException::new);
|
||||
assertEquals("password", password);
|
||||
}
|
||||
|
||||
|
@ -171,8 +155,7 @@ public class OptionalTest {
|
|||
@Test
|
||||
public void whenOrElseWorks_thenCorrect() {
|
||||
String nullName = null;
|
||||
String name = Optional.ofNullable(nullName)
|
||||
.orElse("john");
|
||||
String name = Optional.ofNullable(nullName).orElse("john");
|
||||
assertEquals("john", name);
|
||||
}
|
||||
|
||||
|
@ -180,8 +163,7 @@ public class OptionalTest {
|
|||
@Test
|
||||
public void whenOrElseGetWorks_thenCorrect() {
|
||||
String nullName = null;
|
||||
String name = Optional.ofNullable(nullName)
|
||||
.orElseGet(() -> "john");
|
||||
String name = Optional.ofNullable(nullName).orElseGet(() -> "john");
|
||||
assertEquals("john", name);
|
||||
|
||||
}
|
||||
|
@ -190,13 +172,11 @@ public class OptionalTest {
|
|||
public void whenOrElseGetAndOrElseOverlap_thenCorrect() {
|
||||
String text = null;
|
||||
System.out.println("Using orElseGet:");
|
||||
String defaultText = Optional.ofNullable(text)
|
||||
.orElseGet(this::getMyDefault);
|
||||
String defaultText = Optional.ofNullable(text).orElseGet(this::getMyDefault);
|
||||
assertEquals("Default Value", defaultText);
|
||||
|
||||
System.out.println("Using orElse:");
|
||||
defaultText = Optional.ofNullable(text)
|
||||
.orElse(getMyDefault());
|
||||
defaultText = Optional.ofNullable(text).orElse(getMyDefault());
|
||||
assertEquals("Default Value", defaultText);
|
||||
}
|
||||
|
||||
|
@ -204,13 +184,11 @@ public class OptionalTest {
|
|||
public void whenOrElseGetAndOrElseDiffer_thenCorrect() {
|
||||
String text = "Text present";
|
||||
System.out.println("Using orElseGet:");
|
||||
String defaultText = Optional.ofNullable(text)
|
||||
.orElseGet(this::getMyDefault);
|
||||
String defaultText = Optional.ofNullable(text).orElseGet(this::getMyDefault);
|
||||
assertEquals("Text present", defaultText);
|
||||
|
||||
System.out.println("Using orElse:");
|
||||
defaultText = Optional.ofNullable(text)
|
||||
.orElse(getMyDefault());
|
||||
defaultText = Optional.ofNullable(text).orElse(getMyDefault());
|
||||
assertEquals("Text present", defaultText);
|
||||
}
|
||||
|
||||
|
@ -218,8 +196,7 @@ public class OptionalTest {
|
|||
@Test(expected = IllegalArgumentException.class)
|
||||
public void whenOrElseThrowWorks_thenCorrect() {
|
||||
String nullName = null;
|
||||
String name = Optional.ofNullable(nullName)
|
||||
.orElseThrow(IllegalArgumentException::new);
|
||||
String name = Optional.ofNullable(nullName).orElseThrow(IllegalArgumentException::new);
|
||||
}
|
||||
|
||||
public String getMyDefault() {
|
||||
|
|
|
@ -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,8 +24,7 @@ public class JoinSplitCollectionsUnitTest {
|
|||
public void whenJoiningTwoCollections_thenJoined() {
|
||||
Collection<Integer> collection1 = Arrays.asList(7, 8, 9);
|
||||
Collection<Integer> collection2 = Arrays.asList(10, 11, 12);
|
||||
Collection<Integer> result = Stream.concat(collection1.stream(), collection2.stream())
|
||||
.collect(Collectors.toList());
|
||||
Collection<Integer> result = Stream.concat(collection1.stream(), collection2.stream()).collect(Collectors.toList());
|
||||
|
||||
assertTrue(result.equals(Arrays.asList(7, 8, 9, 10, 11, 12)));
|
||||
}
|
||||
|
@ -35,9 +33,7 @@ public class JoinSplitCollectionsUnitTest {
|
|||
public void whenJoiningTwoCollectionsWithFilter_thenJoined() {
|
||||
Collection<Integer> collection1 = Arrays.asList(7, 8, 11);
|
||||
Collection<Integer> collection2 = Arrays.asList(9, 12, 10);
|
||||
Collection<Integer> result = Stream.concat(collection1.stream(), collection2.stream())
|
||||
.filter(next -> next <= 10)
|
||||
.collect(Collectors.toList());
|
||||
Collection<Integer> result = Stream.concat(collection1.stream(), collection2.stream()).filter(next -> next <= 10).collect(Collectors.toList());
|
||||
|
||||
assertTrue(result.equals(Arrays.asList(7, 8, 9, 10)));
|
||||
}
|
||||
|
@ -45,8 +41,7 @@ public class JoinSplitCollectionsUnitTest {
|
|||
@Test
|
||||
public void whenConvertArrayToString_thenConverted() {
|
||||
String[] colors = new String[] { "Red", "Blue", "Green", "Yellow" };
|
||||
String result = Arrays.stream(colors)
|
||||
.collect(Collectors.joining(", "));
|
||||
String result = Arrays.stream(colors).collect(Collectors.joining(", "));
|
||||
|
||||
assertEquals(result, "Red, Blue, Green, Yellow");
|
||||
}
|
||||
|
@ -54,8 +49,7 @@ public class JoinSplitCollectionsUnitTest {
|
|||
@Test
|
||||
public void whenConvertCollectionToString_thenConverted() {
|
||||
Collection<String> directions = Arrays.asList("Left", "Right", "Top", "Bottom");
|
||||
String result = directions.stream()
|
||||
.collect(Collectors.joining(", "));
|
||||
String result = directions.stream().collect(Collectors.joining(", "));
|
||||
|
||||
assertEquals(result, "Left, Right, Top, Bottom");
|
||||
}
|
||||
|
@ -67,9 +61,7 @@ public class JoinSplitCollectionsUnitTest {
|
|||
users.put(2, "Paul Smith");
|
||||
users.put(3, "Susan Anderson");
|
||||
|
||||
String result = users.entrySet().stream()
|
||||
.map(entry -> entry.getKey() + " = " + entry.getValue())
|
||||
.collect(Collectors.joining(", "));
|
||||
String result = users.entrySet().stream().map(entry -> entry.getKey() + " = " + entry.getValue()).collect(Collectors.joining(", "));
|
||||
|
||||
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("Red", "Blue", "Green", "Yellow"));
|
||||
|
||||
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, "Left-Right-Top-Bottom; Red-Blue-Green-Yellow");
|
||||
}
|
||||
|
@ -91,9 +80,7 @@ public class JoinSplitCollectionsUnitTest {
|
|||
@Test
|
||||
public void whenConvertCollectionToStringAndSkipNull_thenConverted() {
|
||||
Collection<String> fruits = Arrays.asList("Apple", "Orange", null, "Grape");
|
||||
String result = fruits.stream()
|
||||
.filter(Objects::nonNull)
|
||||
.collect(Collectors.joining(", "));
|
||||
String result = fruits.stream().filter(Objects::nonNull).collect(Collectors.joining(", "));
|
||||
|
||||
assertEquals(result, "Apple, Orange, Grape");
|
||||
}
|
||||
|
@ -122,8 +109,7 @@ public class JoinSplitCollectionsUnitTest {
|
|||
@Test
|
||||
public void whenSplitArrayByWordLength_thenConverted() {
|
||||
String[] words = new String[] { "bye", "cold", "it", "and", "my", "word" };
|
||||
Map<Integer, List<String>> result = Arrays.stream(words)
|
||||
.collect(Collectors.groupingBy(String::length));
|
||||
Map<Integer, List<String>> result = Arrays.stream(words).collect(Collectors.groupingBy(String::length));
|
||||
|
||||
assertTrue(result.get(2).equals(Arrays.asList("it", "my")));
|
||||
assertTrue(result.get(3).equals(Arrays.asList("bye", "and")));
|
||||
|
@ -150,9 +136,7 @@ public class JoinSplitCollectionsUnitTest {
|
|||
public void whenConvertStringToMap_thenConverted() {
|
||||
String users = "1 = John Doe, 2 = Paul Smith, 3 = Susan Anderson";
|
||||
|
||||
Map<Integer, String> result = Arrays.stream(users.split(", "))
|
||||
.map(next -> next.split(" = "))
|
||||
.collect(Collectors.toMap(entry -> Integer.parseInt(entry[0]), entry -> entry[1]));
|
||||
Map<Integer, String> result = Arrays.stream(users.split(", ")).map(next -> next.split(" = ")).collect(Collectors.toMap(entry -> Integer.parseInt(entry[0]), entry -> entry[1]));
|
||||
|
||||
assertEquals(result.get(1), "John Doe");
|
||||
assertEquals(result.get(2), "Paul Smith");
|
||||
|
@ -163,10 +147,7 @@ public class JoinSplitCollectionsUnitTest {
|
|||
public void whenConvertCollectionToStringMultipleSeparators_thenConverted() {
|
||||
String fruits = "Apple. , Orange, Grape. Lemon";
|
||||
|
||||
Collection<String> result = Arrays.stream(fruits.split("[,|.]"))
|
||||
.map(String::trim)
|
||||
.filter(next -> !next.isEmpty())
|
||||
.collect(Collectors.toList());
|
||||
Collection<String> result = Arrays.stream(fruits.split("[,|.]")).map(String::trim).filter(next -> !next.isEmpty()).collect(Collectors.toList());
|
||||
|
||||
assertTrue(result.equals(Arrays.asList("Apple", "Orange", "Grape", "Lemon")));
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue