-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathCommonMethodsDemo.java
More file actions
198 lines (140 loc) · 4.89 KB
/
CommonMethodsDemo.java
File metadata and controls
198 lines (140 loc) · 4.89 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package Collections.Practice;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
public class CommonMethodsDemo {
public static void main(String[] args) {
System.out.println("=== COLLECTION COMMON METHODS DEMO ===\n");
/*
IMPORTANT CONCEPT:
We are using the Collection interface as the reference type.
The object type is ArrayList.
*/
Collection<String> fruits = new ArrayList<>();
// ------------------------------
// add()
// ------------------------------
System.out.println("Adding elements using add()");
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Orange");
fruits.add("Mango");
System.out.println("Fruits: " + fruits);
// ------------------------------
// size()
// ------------------------------
System.out.println("\nChecking size of collection");
int size = fruits.size();
System.out.println("Size: " + size);
// ------------------------------
// contains()
// ------------------------------
System.out.println("\nChecking if collection contains certain items");
boolean hasApple = fruits.contains("Apple");
boolean hasGrape = fruits.contains("Grape");
System.out.println("Contains Apple? " + hasApple);
System.out.println("Contains Grape? " + hasGrape);
// ------------------------------
// remove()
// ------------------------------
System.out.println("\nRemoving an element");
fruits.remove("Banana");
System.out.println("After removing Banana:");
System.out.println(fruits);
// ------------------------------
// addAll()
// ------------------------------
System.out.println("\nAdding multiple elements using addAll()");
List<String> moreFruits = new ArrayList<>();
moreFruits.add("Pineapple");
moreFruits.add("Strawberry");
moreFruits.add("Peach");
fruits.addAll(moreFruits);
System.out.println("After addAll:");
System.out.println(fruits);
// ------------------------------
// Iterating through Collection
// ------------------------------
System.out.println("\nLooping through collection:");
for (String fruit : fruits) {
System.out.println(fruit);
}
// ------------------------------
// isEmpty()
// ------------------------------
System.out.println("\nChecking if collection is empty");
boolean empty = fruits.isEmpty();
System.out.println("Is empty? " + empty);
// ------------------------------
// clear()
// ------------------------------
System.out.println("\nClearing the collection");
fruits.clear();
System.out.println("After clear:");
System.out.println(fruits);
System.out.println("\nCheck if empty after clear:");
System.out.println("Is empty? " + fruits.isEmpty());
// ------------------------------
// TODO Exploration Section
// ------------------------------
/*
TODO 1:
Create a new Collection called numbers
Add the following values:
10, 20, 30, 40, 50
*/
Collections<Integer> numbers = new ArrayList<>();
numbers.add(10);
numbers.add(20);
numbers.add(30);
numbers.add(40);
numbers.add(50);
// num.addAll(numbers);
/*
TODO 2:
Print the size of the numbers collection
*/
System.out.println(numbers.size());
/*
TODO 3:
Check if the collection contains 30
*/
boolean hasNum= numbers.contains(30);
/*
TODO 4:
Remove the number 20
*/
int newNum = numbers.remove(20);
/*
TODO 5:
Loop through the numbers collection
and print each value
*/
for(int n:numbers){
System.out.println(n);
}
/*
REFLECTION QUESTIONS:
1. Why can we use Collection as the reference type?
Because ArrayList implements the Collection interface.
ArrayList is a class that implements List, and List extends Collection.
So an ArrayList is a type of Collection, which allows us to store it in a Collection reference.
2. What methods are available because of the Collection interface?
add()
remove()
contains()
size()
isEmpty()
clear()
iterator()
3. What methods are NOT available when using Collection instead of List?
Methods that belong only to List are not available when the reference type is Collection.
Examples of List-only methods:
get(index)
set(index, element)
add(index, element)
remove(index)
indexOf()
*/
}
}