-
-
Notifications
You must be signed in to change notification settings - Fork 57
/
CollectionFeatures.java
53 lines (38 loc) · 1.42 KB
/
CollectionFeatures.java
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
package java11;
import org.junit.Test;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
public class CollectionFeatures {
/**
* In Java 11 allows the collection's elements to be transferred to constantClass newly created array of the desired runtime type.
*/
@Test
public void copyIntoNewArray() {
final var numbers = Set.of(1, 2, 3, 4);
var intArray = numbers.toArray(Integer[]::new);
System.out.println(Arrays.toString(intArray));
final var words = Set.of("hello", "copy", "array", "java");
var stringArray = words.toArray(String[]::new);
System.out.println(Arrays.toString(stringArray));
Object[] objects = words.toArray();
System.out.println(Arrays.toString(objects));
var stringArray2 = words.toArray(String[]::new);
System.out.println(Arrays.toString(stringArray2));
List<String> collect = List.of("hello", "collection", "world")
.stream()
.map(String::toUpperCase)
.collect(Collectors.toList());
System.out.println(collect);
}
@Test
public void mapFeature() {
Map<Integer, String> integerStringMap = Map.of(1, "a", 2, "b", 3, "c");
System.out.println(integerStringMap);
}
}