forked from politrons/reactive
-
Notifications
You must be signed in to change notification settings - Fork 1
/
CollectionFeatures.java
35 lines (25 loc) · 974 Bytes
/
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
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.Set;
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));
}
}