-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathJava9Features.java
More file actions
97 lines (85 loc) · 3.05 KB
/
Copy pathJava9Features.java
File metadata and controls
97 lines (85 loc) · 3.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package java9;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInfo;
import org.junit.jupiter.api.TestMethodOrder;
/**
* New JAVA 9 Features: TakeWhile, DropWhile, Immutable List, Immutable Set, Immutable Map, List.of(), Set.of(), Map.of() examples.
*/
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class Java9Features {
List<Integer> numbers = new ArrayList<>();
@BeforeEach
public void setup(TestInfo testInfo) {
System.out.println(testInfo.getDisplayName());
Collections.addAll(numbers, 1, 2, 4, 5, 7, 6, 8, 3, 9, 10);
}
@AfterEach
public void tearDown() {
numbers.clear();
}
/**
* TakeWhile: It processes the elements until the condition satisfies. Then stops processing.
* That's why it does not process 3 even it is smaller than 6.
*/
@Test
@Order(1)
public void takeWhileExample() {
numbers.stream()
.takeWhile(numbers -> numbers < 6) //Prints: 1 2 4 5
.forEach(System.out::println);
}
/**
* DropWhile: It does not process the elements which do not satisfy the condition.
* Then starts to process the other elements regardless of conditions. That's why it processes 3 even it is smaller than 6.
*/
@Test
@Order(2)
public void dropWhileExample() {
numbers.stream()
.dropWhile(numbers -> numbers < 6) //Prints: 7 6 8 3 9 10
.forEach(System.out::println);
}
/**
* Immutable List: List.of() declaration creates a list which is an immutable (constant) list.
*/
@Test
@Order(3)
public void immutableList() {
List<Integer> listOf = List.of(1, 2, 3, 4, 5, 6);
System.out.println(listOf);
//List.of() creates immutable (constant) list.
Assertions.assertThrows(UnsupportedOperationException.class, () -> listOf.add(7));
}
/**
* Immutable Set: Set.of() declaration creates a set which is an immutable (constant) set.
*/
@Test
@Order(4)
public void immutableSet() {
Set<Integer> setOf = Set.of(1, 2, 3, 4, 5, 6);
System.out.println(setOf); //Note: Set does not maintain the order.
//Set.of() creates immutable (constant) set.
Assertions.assertThrows(UnsupportedOperationException.class, () -> setOf.add(7));
}
/**
* Immutable Map: Map.of() declaration creates a map which is an immutable (constant) map.
*/
@Test
@Order(5)
public void immutableMap() {
Map<Integer, String> mapOf = Map.of(1, "Ronaldo", 2, "Messi");
System.out.println(mapOf);
//Map.of() creates immutable (constant) map.
Assertions.assertThrows(UnsupportedOperationException.class, () -> mapOf.put(3, "Ozil"));
}
}