forked from janbodnar/Java-Advanced
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenericConcatListEx.java
More file actions
37 lines (25 loc) · 851 Bytes
/
GenericConcatListEx.java
File metadata and controls
37 lines (25 loc) · 851 Bytes
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
package com.zetcode;
import java.util.ArrayList;
import java.util.List;
public class GenericConcatListEx {
public static void main(String[] args) {
var vals1 = List.of(1, 2, 3);
var vals2 = List.of(4, 5, 6);
var vals3 = List.of(7, 8, 9);
var vals = concatenate(vals1, vals2, vals3);
System.out.println(vals);
var words1 = List.of("pen", "paper", "ink");
var words2 = List.of("sky", "cloud", "wind");
var words3 = List.of("tree", "forest", "wood");
var words = concatenate(words1, words2, words3);
System.out.println(words);
}
@SafeVarargs
private static <T> List<T> concatenate(List<T>... lists) {
return new ArrayList<>() {{
for (List<T> mylist : lists) {
addAll(mylist);
}
}};
}
}