-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c1ad391
commit ea3f790
Showing
5 changed files
with
140 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.finance.programming; | ||
|
||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.List; | ||
import java.util.function.Predicate; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertIterableEquals; | ||
|
||
public class FilterAList { | ||
|
||
/** | ||
* Filtering a list Description: Filter a list of integers to keep only the even numbers using streams and Predicate. | ||
*/ | ||
|
||
@Test | ||
@DisplayName("Test Even Numbers Filter") | ||
void testEvenNumbers(){ | ||
List<Integer> givenNumbers = List.of(1, 3, 4, 10, 20, 23, 15, 17, 19, 402); | ||
List<Integer> expectedNumbers = List.of(4, 10, 20, 402); | ||
|
||
Predicate<Integer> filterEvenNumbers = number -> number % 2 == 0; | ||
|
||
List<Integer> actualNumbers = givenNumbers.stream() | ||
.filter(filterEvenNumbers) | ||
.toList(); | ||
|
||
assertIterableEquals(expectedNumbers, actualNumbers, | ||
"The content of the arrays is not the same"); | ||
|
||
assertEquals(expectedNumbers.size(), actualNumbers.size(), | ||
"The size of the arrays does not match"); | ||
|
||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.finance.programming; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.List; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
public class MappingAList { | ||
|
||
|
||
/** | ||
* Take a list of strings and use map to convert all strings to uppercase. | ||
*/ | ||
|
||
|
||
@Test | ||
void testStringToUpperCase(){ | ||
List<String> givenList = List.of("Spring", "java", "microservices", "aws", "api", "kubernetes"); | ||
List<String> expectedList = List.of("SPRING", "JAVA", "MICROSERVICES", "AWS", "API", "KUBERNETES"); | ||
|
||
List<String> actualList = givenList.stream() | ||
.map(String::toUpperCase) | ||
.toList(); | ||
|
||
assertIterableEquals(expectedList, actualList, | ||
"The lists does not match their contents"); | ||
|
||
assertEquals(expectedList.get(1), actualList.get(1), | ||
"The content of the given index does not match"); | ||
|
||
assertNotEquals(expectedList, givenList, | ||
"The list match its content, the content must not match"); | ||
|
||
assertEquals(expectedList.size(), actualList.size(), | ||
"The size does not match"); | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.finance.programming; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.List; | ||
import java.util.function.BinaryOperator; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
public class ReduceAList { | ||
|
||
/** | ||
* Use reduce to sum all the elements of a list of integers. | ||
*/ | ||
|
||
@Test | ||
void testReduceToASumOfNumbers(){ | ||
List<Integer> givenList = List.of(1, 2, 3, 4, 5); | ||
int expectedSum = 15; | ||
|
||
// BinaryOperator<Integer> findASum = (x, y) -> x + y; | ||
BinaryOperator<Integer> findASum = Integer::sum; | ||
|
||
|
||
int actualSum = givenList.stream() | ||
.reduce(findASum) | ||
.orElse(0); | ||
|
||
assertEquals(expectedSum, actualSum, | ||
"The result does not match"); | ||
|
||
assertNotEquals(10, actualSum, | ||
"The result match, it must not be the same"); | ||
|
||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters