-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArrayListDemo2.java
70 lines (55 loc) · 1.93 KB
/
ArrayListDemo2.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;
public class ArrayListDemo2 {
public static void main(String args[]) {
// create an array list
List<Integer> al = new ArrayList<>();
System.out.println("Initial size of al: " + al.size());
// add elements to the array list
al.add(10);
al.add(20);
al.add(30);
al.add(40);
al.add(50);
// foreach iterate
for (Integer i : al) {
System.out.print(i + " ");
}
System.out.println();
al.forEach(e -> System.out.println(e));
al.remove(2); // remove using index
al.remove(new Integer(10)); // remove using value
// iterate through Iterator
Iterator<Integer> itr = al.iterator();
while (itr.hasNext()) {
System.out.print(itr.next() + " ");
}
System.out.println();
// convert to array
Integer ia[] = new Integer[al.size()];
al.toArray(ia);
// iterate array
for (int i = 0; i < ia.length; i++) {
System.out.print(ia[i] + " ");
}
System.out.println();
// convert array to list
List<Integer> alist = Arrays.asList(ia);
// foreach iterate
for (Integer i : alist) {
System.out.print(i + " ");
}
System.out.println();
// streams
List<Integer> streamList = Arrays.stream(ia).collect(Collectors.toList());
streamList.forEach(e -> System.out.print(e + " "));
System.out.println();
// primitive type array to list
int[] a = {10, 20, 30, 40, 50};
List<Integer> integerList = Arrays.stream(a).boxed().collect(Collectors.toList());
integerList.forEach(e -> System.out.print(e + " "));
}
}