-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathCollectionBasics.java
More file actions
162 lines (127 loc) · 3.62 KB
/
CollectionBasics.java
File metadata and controls
162 lines (127 loc) · 3.62 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
package Collections.Practice;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
public class CollectionBasics {
public static void main(String[] args) {
Collection<Integer> numbers = new ArrayList<>();
filterGreaterThanTwenty(numbers);
numbers.add(4);
numbers.add(4);
numbers.add(15);
numbers.add(16);
numbers.add(23);
numbers.add(42);
System.out.println("Sum of numbers: " + sum(numbers));
System.out.println("Count of even numbers: " + countEven(numbers));
System.out.println("Largest number: " + findMax(numbers));
System.out.println("Contains duplicates? " + hasDuplicates(numbers));
}
/*
PROBLEM 1
Return the sum of all numbers in the collection
*/
public static int sum(Collection<Integer> numbers) {
int total = 0;
for (int i: numbers){
total+=i;
}
// TODO:
// Loop through the collection
// Add each number to total
return total;
}
/*
PROBLEM 2
Count how many numbers are even
*/
public static int countEven(Collection<Integer> numbers) {
int count = 0;
for (int i = 0; i<=numbers.size(); ++i){
if (i %2==0){
count++;
System.out.println(count);
}
}
// TODO:
// Loop through the collection
// If the number is even, increase count
return count;
}
/*
PROBLEM 3
Find the largest number in the collection
*/
public static int findMax(Collection<Integer> numbers) {
int max = Integer.MIN_VALUE;
for (int num: numbers){
if (max < num){
max= num;
}else {
max = Integer.MIN_VALUE;
}
}
// TODO:
// Loop through numbers
// Update max if current number is larger
return max;
}
/*
PROBLEM 4
Return true if the collection contains duplicates
Return false otherwise
*/
public static boolean hasDuplicates(Collection<Integer> numbers) {
// TODO:
// Hint:
// Compare the size of a collection with the size of a Set
Set<Integer> numbersset= new HashSet<>();
numbersset.add(4);
numbersset.add(4);
numbersset.add(15);
numbersset.add(16);
numbersset.add(23);
numbersset.add(42);
for (int i = 0; i< numbers.size();++i){
for (int e = 0; e < numbersset.size(); ++i ){
if (i==e){
return true;
}
}
}
return false;
}
/*
PROBLEM 5
Count how many times a target value appears
*/
public static int countOccurrences(Collection<Integer> numbers, int target) {
int count = 0;
// TODO:
// Loop through numbers
// If number equals target, increase count
for (int i=0; i==target; ++i){
++count;
}
return count;
}
/*
BONUS PROBLEM
Create and return a new collection
that only contains numbers greater than 20
*/
public static Collection<Integer> filterGreaterThanTwenty(Collection<Integer> numbers) {
Collection<Integer> result = new ArrayList<>();
for (int num:numbers){
if (num>20){
result.add(num);
}
}
// TODO:
// Loop through numbers
// Add numbers greater than 20 to result
System.out.println(result);
return result;
}
}