Skip to content

Commit 021096b

Browse files
Merge pull request alexandregama#35 from craft-coder/java8-stream-sorted
Java 8 - Creates two examples of how to use Streams Sorting its list by using Java 8 closed alexandregama#13
2 parents bf35035 + 75abf7a commit 021096b

File tree

5 files changed

+151
-0
lines changed

5 files changed

+151
-0
lines changed

java8-stream-sorted/.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-sorted/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-sorted/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-sorted</artifactId>
7+
<version>0.0.1-SNAPSHOT</version>
8+
<packaging>jar</packaging>
9+
10+
<name>java8-stream-sorted</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: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.craftcoder.java8.defaultmethod;
2+
3+
import static org.hamcrest.MatcherAssert.assertThat;
4+
import static org.hamcrest.Matchers.equalTo;
5+
6+
import org.junit.Ignore;
7+
import org.junit.Test;
8+
9+
public class DefaultMethodTest {
10+
11+
/**
12+
* In this example, we don't need to use the default method send() from PaymentService interface
13+
*/
14+
@Ignore
15+
@Test
16+
public void shouldRetrieveTheDefaultFees() throws Exception {
17+
PaymentService service = new PayPalPaymentService();
18+
19+
double fees = service.retrieveDefaultFees();
20+
21+
assertThat(fees, equalTo(10.9));
22+
}
23+
24+
@Test
25+
public void shouldInvokeTheDefaultMethodFromPaymentService() throws Exception {
26+
PaymentService paymentService = new PayPalPaymentService();
27+
28+
double valueSent = paymentService.send(20);
29+
30+
assertThat(valueSent, equalTo(20.0));
31+
}
32+
33+
}
34+
35+
36+
interface PaymentService {
37+
38+
double retrieveDefaultFees();
39+
40+
default double send(double value) {
41+
System.out.println("Sending the value: " + value);
42+
43+
return value;
44+
}
45+
46+
}
47+
48+
class PayPalPaymentService implements PaymentService {
49+
50+
@Override
51+
public double retrieveDefaultFees() {
52+
return 10.9;
53+
}
54+
55+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.craftcoder.java8.stream.sorted;
2+
3+
import static org.hamcrest.MatcherAssert.assertThat;
4+
import static org.hamcrest.Matchers.contains;
5+
6+
import java.util.Arrays;
7+
import java.util.Comparator;
8+
import java.util.List;
9+
import java.util.stream.Collectors;
10+
11+
import org.junit.Test;
12+
13+
public class StreamWithSortedTest {
14+
15+
@Test
16+
public void shouldSortTheList() throws Exception {
17+
List<String> listOfWords = Arrays.asList("B", "A", "D", "E", "C");
18+
19+
List<String> sortedList = listOfWords
20+
.stream()
21+
.sorted()
22+
.collect(Collectors.toList());
23+
24+
assertThat(sortedList, contains("A", "B", "C", "D", "E"));
25+
}
26+
27+
@Test
28+
public void shouldSortTheListWithInvertedComparator() throws Exception {
29+
List<String> listOfWords = Arrays.asList("B", "A", "D", "E", "C");
30+
31+
Comparator<String> inverted = (String o1, String o2) -> o2.compareTo(o1);
32+
33+
List<String> sortedList = listOfWords
34+
.stream()
35+
.sorted(inverted)
36+
.collect(Collectors.toList());
37+
38+
assertThat(sortedList, contains("E", "D", "C", "B", "A"));
39+
}
40+
41+
}

0 commit comments

Comments
 (0)