public class CreatingArrayLists
1: //CreatingArrayLists.java
2: //Illustrates various ways to create an ArrayList.
4: import java.util.ArrayList;
5: import java.util.Arrays;
6: import java.util.List;
7: import java.util.stream.Collectors;
8: import java.util.stream.Stream;
10: public class CreatingArrayLists
11: {
12: public static void main(String[] args)
13: {
14: //First we create an array of integers for use in some of
15: //the following examples, and then display its values.
16: Integer[] arrayOfInteger =
17: {
18: 4, 1, 5, 3, 2
19: };
20: System.out.print("arrayOfInteger: ");
21: for (int i : arrayOfInteger)
22: {
23: System.out.print(i + " ");
24: }
25: System.out.println();
27: //1Now we create an ArrayList from the above array using
28: //the static method Arrays.asList() from the Arrays class:
29: ArrayList<Integer> arrayListOfInteger1 =
30: new ArrayList<>(Arrays.asList(arrayOfInteger));
31: System.out.println("arrayListOfInteger1: " + arrayListOfInteger1);
33: //2Next we create an ArrayList from the above array using
34: //the Java 8 Stream "collector" approach:
35: ArrayList<Integer> arrayListOfInteger2 =
36: Stream
37: .of(arrayOfInteger)
38: .collect(Collectors.toCollection(ArrayList::new));
39: System.out.println("arrayListOfInteger2: " + arrayListOfInteger2);
41: //3In this code segment the last line "proves" that if we use an
42: //interface as the type rather than the class, we still get the
43: //class object we expect.
44: List<Integer> arrayListOfInteger3 =
45: Stream
46: .of(arrayOfInteger)
47: .collect(Collectors.toCollection(ArrayList::new));
48: System.out.println("arrayListOfInteger3: " + arrayListOfInteger3);
49: System.out.println(arrayListOfInteger3.getClass().getName());
51: //4With Java 9 we can shorten the above approach to this:
52: List<Integer> arrayListOfInteger4 = List.of(4, 1, 5, 3, 2);
53: System.out.println("arrayListOfInteger4: " + arrayListOfInteger4);
54: System.out.println(arrayListOfInteger4.getClass().getName());
55: //But note that we get a somewhat different class ...
57: //5We can now also intialize an ArrayList directly with a code block:
58: ArrayList<Integer> arrayListOfInteger5 = new ArrayList<Integer>()
59: {
60: {
61: add(4);
62: add(1);
63: add(5);
64: add(3);
65: add(2);
66: }
67: };
68: System.out.println("arrayListOfInteger5: " + arrayListOfInteger5);
70: System.out.println();
72: //========================================================
73: //Exercise:
74: //Repeat what we did in the above five code segments, but
75: //with strings instead of integers, and here is a starting
76: //array of strings to use:
77: String arrayOfString[] =
78: {
79: "klm", "abc", "xyz", "pqr"
80: };
81: System.out.print("arrayOfString: ");
82: for (String s : arrayOfString)
83: {
84: System.out.print(s + " ");
85: }
86: System.out.println();
87: }
88: }
89: /* Output:
90: arrayOfInteger: 4 1 5 3 2
91: arrayListOfInteger1: [4, 1, 5, 3, 2]
92: arrayListOfInteger2: [4, 1, 5, 3, 2]
93: arrayListOfInteger3: [4, 1, 5, 3, 2]
94: java.util.ArrayList
95: arrayListOfInteger4: [4, 1, 5, 3, 2]
96: java.util.ImmutableCollections$ListN
97: arrayListOfInteger5: [4, 1, 5, 3, 2]
99: arrayOfString: klm abc xyz pqr
100: */