Skip to content

Commit 3238c65

Browse files
committed
Creates a few tests to show how to use Streams with Match from Java 8
1 parent 021096b commit 3238c65

File tree

4 files changed

+101
-0
lines changed

4 files changed

+101
-0
lines changed

java8-stream-match/.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/target/
2+
build/
3+
.classpath
4+
.project
5+
.settings

java8-stream-match/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# java8-guides-tutorials
2+
Java 8 - Guides and Tutorias

java8-stream-match/pom.xml

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>com.craftcoder.java8</groupId>
6+
<artifactId>java8-stream-match</artifactId>
7+
<version>0.0.1-SNAPSHOT</version>
8+
<packaging>jar</packaging>
9+
10+
<name>java8-stream-match</name>
11+
<url>http://maven.apache.org</url>
12+
13+
<properties>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
</properties>
16+
17+
<dependencies>
18+
<!-- TESTING DEPENDENCIES -->
19+
<dependency>
20+
<groupId>org.hamcrest</groupId>
21+
<artifactId>hamcrest-all</artifactId>
22+
<version>1.3</version>
23+
</dependency>
24+
25+
<dependency>
26+
<groupId>junit</groupId>
27+
<artifactId>junit</artifactId>
28+
<version>4.12</version>
29+
<scope>test</scope>
30+
</dependency>
31+
<!-- TESTING DEPENDENCIES -->
32+
33+
</dependencies>
34+
35+
<build>
36+
<plugins>
37+
<plugin>
38+
<groupId>org.apache.maven.plugins</groupId>
39+
<artifactId>maven-eclipse-plugin</artifactId>
40+
<configuration>
41+
<downloadSources>true</downloadSources>
42+
<downloadJavadocs>true</downloadJavadocs>
43+
</configuration>
44+
</plugin>
45+
</plugins>
46+
</build>
47+
48+
</project>
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.craftcoder.java8.stream.match;
2+
3+
import static org.hamcrest.MatcherAssert.assertThat;
4+
import static org.hamcrest.Matchers.is;
5+
6+
import java.util.Arrays;
7+
import java.util.List;
8+
9+
import org.junit.Test;
10+
11+
public class StreamWithMatchTest {
12+
13+
@Test
14+
public void shouldCheckIfThereIsANumberGreaterThan4() throws Exception {
15+
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
16+
17+
boolean anyNumberGreaterThan4 = numbers
18+
.stream()
19+
.anyMatch(number -> number > 4);
20+
21+
assertThat(anyNumberGreaterThan4, is(true));
22+
}
23+
24+
@Test
25+
public void shouldCheckIfEachNumberIsPair() throws Exception {
26+
List<Integer> numbers = Arrays.asList(2, 4, 6);
27+
28+
boolean eachNumberIsPair = numbers
29+
.stream()
30+
.allMatch(number -> number % 2 == 0);
31+
32+
assertThat(eachNumberIsPair, is(true));
33+
}
34+
35+
@Test
36+
public void shouldCheckIfEachNumberIsNotPair() throws Exception {
37+
List<Integer> numbers = Arrays.asList(3, 5, 7);
38+
39+
boolean eachNumberIsNotPair = numbers
40+
.stream()
41+
.noneMatch(number -> number % 2 == 0);
42+
43+
assertThat(eachNumberIsNotPair, is(true));
44+
}
45+
46+
}

0 commit comments

Comments
 (0)