Skip to content

Commit 8d2b902

Browse files
committed
Java 9 additions to Stream, Optional and Arrays
1 parent e7ad274 commit 8d2b902

5 files changed

Lines changed: 274 additions & 0 deletions

File tree

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Includes all examples about Java 9 new features
1010
* `mvn test`
1111

1212
Find more detail at:
13+
* [Java 9 Tutorial](https://howtoprogram.xyz/java-9-tutorial/)
1314
* [Set Up Eclipse, IntelliJ And NetBeans For Java 9](https://howtoprogram.xyz/2017/09/24/set-up-eclipse-intellij-and-netbeans-for-java-9/)
1415
* [Create Immutable Lists In Java 9 By Static Factory Methods](https://howtoprogram.xyz/2017/09/24/java-9-create-immutable-lists-static-factory-method/)
1516
* [Java 9 Example With Maven And JUnit 5](https://howtoprogram.xyz/2017/09/23/java-9-maven-junit-5-example/)

‎java9-language-features-examples/README.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ Demonstration for:
66

77
[Copy Streams In Java 9 With The InputStream.transferTo() Method](https://howtoprogram.xyz/2017/10/01/java-9-inputstream-transferto-copy-streams/)
88

9+
[Compare Arrays In Java 9](https://howtoprogram.xyz/2017/10/14/java-9-compare-arrays/)
10+
11+
[Streams API Updates In Java 9](https://howtoprogram.xyz/2017/10/12/java-9-streams-api-updates/)
912

1013
## Prerequisites:
1114
* Maven >= 3.3.1
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package com.howtoprogram.java9ex;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.util.Arrays;
6+
7+
import static org.junit.jupiter.api.Assertions.assertEquals;
8+
import static org.junit.jupiter.api.Assertions.assertFalse;
9+
import static org.junit.jupiter.api.Assertions.assertTrue;
10+
11+
public class Java9ArrayCompareTest {
12+
13+
@Test
14+
public void arrayEqualsTest() {
15+
16+
int[] existRows = {0, 1, 2, 3, 4, 5};
17+
int[] newRows = {3, 4, 5, 1, 2, 0};
18+
19+
assertFalse(Arrays.equals(existRows, newRows));
20+
21+
assertTrue(Arrays.equals(existRows, 1, 3, newRows, 3, 5));
22+
assertTrue(Arrays.equals(existRows, 3, 5, newRows, 0, 2));
23+
24+
}
25+
26+
@Test
27+
public void compareArraysTest() {
28+
29+
30+
int[] tomMarks = {5, 6, 7, 8, 9, 10};
31+
32+
int[] aliceMarks = {5, 6, 7, 8, 9, 10};
33+
34+
int[] daisyMarks = {5, 6, 7, 10, 9, 10};
35+
36+
int[] maryMarks = {5, 6, 7, 8};
37+
38+
39+
assertEquals(0, Arrays.compare(tomMarks, aliceMarks));
40+
41+
assertEquals(-1, Arrays.compare(tomMarks, daisyMarks));
42+
43+
assertEquals(2, Arrays.compare(tomMarks, maryMarks));
44+
}
45+
46+
@Test
47+
public void compareSliceArraysTest() {
48+
49+
50+
int[] tomMarks = {5, 6, 7, 8, 9, 10};
51+
52+
int[] daisyMarks = {5, 6, 7, 10, 9, 10};
53+
54+
int[] maryMarks = {5, 6, 7, 8};
55+
56+
57+
assertEquals(0, Arrays.compare(tomMarks, 0, 3,
58+
daisyMarks, 0, 3));
59+
60+
assertEquals(0, Arrays.compare(tomMarks, 0, 4,
61+
maryMarks, 0, maryMarks.length));
62+
63+
assertEquals(1, Arrays.compare(daisyMarks, 0, 4,
64+
maryMarks, 0, maryMarks.length));
65+
}
66+
67+
68+
@Test
69+
public void mismatchArraysTest() {
70+
71+
72+
int[] a = {1, 2, 3, 4, 5};
73+
int[] b = {1, 2, 3, 4, 5};
74+
int[] c = {1, 2, 4, 4, 5, 6};
75+
76+
assertEquals(-1, Arrays.mismatch(a, b));
77+
78+
assertEquals(2, Arrays.mismatch(a, c));
79+
80+
assertEquals(-1, Arrays.mismatch(a, 0, 2, c, 0, 2));
81+
82+
assertEquals(2, Arrays.mismatch(a, 0, 3, c, 0, 3));
83+
84+
//mismatch return relative index
85+
assertEquals(0, Arrays.mismatch(a, 2, a.length, c, 2, 5));
86+
}
87+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.howtoprogram.java9ex;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.api.Test;
5+
6+
import java.util.stream.IntStream;
7+
import java.util.stream.Stream;
8+
9+
import static org.junit.jupiter.api.Assertions.assertEquals;
10+
import static org.junit.jupiter.api.Assertions.assertTrue;
11+
12+
public class Java9NewStreamAPITest {
13+
14+
@Test
15+
public void dropWhileTest() {
16+
17+
Stream<Integer> stdMarks = Stream.of(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
18+
Stream<Integer> goodMarks = stdMarks.dropWhile(s -> s < 5);
19+
20+
goodMarks.forEach(System.out::println);
21+
System.out.println("--------------------------------");
22+
23+
Stream<Integer> givenMarks = Stream.of(1, 2, 10, 9, 5, 6, 2, 3);
24+
Stream<Integer> remainMarks = givenMarks.dropWhile(s -> s < 5);
25+
26+
remainMarks.forEach(System.out::println);
27+
}
28+
29+
@Test
30+
public void takeWhileTest() {
31+
32+
Stream<Integer> stdMarks = Stream.of(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
33+
Stream<Integer> ngMarks = stdMarks.takeWhile(s -> s < 5);
34+
35+
ngMarks.forEach(System.out::println);
36+
System.out.println("--------------------------------");
37+
38+
Stream<Integer> givenMarks = Stream.of(1, 2, 10, 9, 5, 6, 2, 3);
39+
Stream<Integer> remainMarks = givenMarks.takeWhile(s -> s < 5);
40+
41+
remainMarks.forEach(System.out::println);
42+
}
43+
44+
@Test
45+
public void streamTest() {
46+
Stream.iterate(0, n -> n + 1)
47+
.forEach(System.out::println);
48+
System.out.println("--------------------------------");
49+
Stream.iterate(0, n -> n < 100, n -> n + 1)
50+
.forEach(System.out::println);
51+
}
52+
53+
@Test
54+
public void ofNullableTest() {
55+
56+
String username = null;
57+
Stream st = Stream.ofNullable(username);
58+
59+
assertEquals(0, st.count());
60+
61+
username ="admin";
62+
st = Stream.ofNullable(username);
63+
64+
assertEquals(1, st.count());
65+
66+
}
67+
68+
}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
package com.howtoprogram.optional;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.api.Test;
5+
6+
import java.io.*;
7+
import java.util.HashMap;
8+
import java.util.List;
9+
import java.util.Map;
10+
import java.util.Optional;
11+
12+
import static org.junit.jupiter.api.Assertions.assertEquals;
13+
import static org.junit.jupiter.api.Assertions.assertThrows;
14+
import static org.junit.jupiter.api.Assertions.assertTrue;
15+
16+
17+
public class Java9OptionalNewMethodsTest {
18+
19+
@Test
20+
public void testOptionalIfPresentOrElse() {
21+
22+
List<Optional<String>> days = List.of(Optional.of("Monday"),
23+
Optional.of("Tuesday"),
24+
Optional.empty(),
25+
Optional.ofNullable(null),
26+
Optional.of("Friday"),
27+
Optional.of("Saturday"),
28+
Optional.of("Sunday")
29+
);
30+
days.stream().forEach(p -> p.ifPresentOrElse(System.out::println,
31+
() -> System.out.println("Day not available")));
32+
}
33+
34+
@Test
35+
public void testOptionalOr() {
36+
37+
List<Optional<Integer>> studentAges = List.of(Optional.of(20),
38+
Optional.of(21),
39+
Optional.empty(),
40+
Optional.ofNullable(null),
41+
Optional.of(22),
42+
Optional.of(18),
43+
Optional.of(19)
44+
);
45+
studentAges.stream().map(p -> p.or(() -> Optional.of(20)))
46+
.forEach(System.out::println);
47+
}
48+
49+
@Test
50+
public void testOptionalStream() {
51+
52+
Optional<String> maleOpt = Optional.of("Male");
53+
Optional<String> feMaleOpt = Optional.of("FeMale");
54+
Optional<String> OtherOpt = Optional.empty();
55+
56+
assertEquals("Male",maleOpt.stream().findFirst().get());
57+
assertTrue(maleOpt.stream().count() == 1);
58+
59+
assertTrue(OtherOpt.stream().count() == 0);
60+
}
61+
62+
@Test
63+
public void testImmutableListOrdered() {
64+
65+
List<?> coffeeList = List.of("Espresso", "Latte", "Cappuccino");
66+
67+
assertEquals("Espresso", coffeeList.get(0));
68+
69+
assertEquals(2, coffeeList.indexOf("Cappuccino"));
70+
71+
}
72+
73+
@Test
74+
public void testImmutableListMutableElements() {
75+
76+
Map<Integer, String> numbersMap = new HashMap<>();
77+
numbersMap.put(1, "One");
78+
numbersMap.put(2, "Two");
79+
80+
Map<Integer, String> currencyMap = new HashMap<>();
81+
currencyMap.put(100, "Hundreds");
82+
currencyMap.put(1000, "Thousands");
83+
84+
List<Map<Integer, String>> numCurrList = List.of(numbersMap, currencyMap);
85+
86+
assertEquals(2, numCurrList.get(0).size());
87+
88+
//Add more elements to the mutable element of the List
89+
numbersMap.put(3, "Three");
90+
numCurrList.get(0).put(4, "Four");
91+
92+
assertEquals(4, numCurrList.get(0).size());
93+
94+
}
95+
96+
@Test
97+
public void testImmutableListSerializable() throws IOException {
98+
99+
List<String> drinks = List.of("Coffee", "Wine", "Fruit");
100+
//serialize the list
101+
ObjectOutputStream oos = new ObjectOutputStream(System.out);
102+
oos.writeObject(drinks);
103+
104+
//create a list of non-serializable objects
105+
List<OutputStream> outputStreamList = List.of(new ByteArrayOutputStream(),
106+
new PrintStream(System.out));
107+
108+
assertThrows(NotSerializableException.class, () -> {
109+
110+
oos.writeObject(outputStreamList);
111+
}
112+
);
113+
}
114+
115+
}

0 commit comments

Comments
 (0)