Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions src/main/java/Collections/Practice/CollectionBasics.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.ArrayList;
import java.util.Collection;
import java.util.Set;

public class CollectionBasics {
public static void main(String[] args) {
Expand Down Expand Up @@ -34,6 +35,9 @@ public static int sum(Collection<Integer> numbers) {
// Loop through the collection
// Add each number to total

for (int num : numbers) {
total += num;
}
return total;
}

Expand All @@ -49,7 +53,11 @@ public static int countEven(Collection<Integer> numbers) {
// TODO:
// Loop through the collection
// If the number is even, increase count

for (int num : numbers) {
if (num % 2 == 0) {
count++;
}
}
return count;
}

Expand All @@ -65,7 +73,11 @@ public static int findMax(Collection<Integer> numbers) {
// TODO:
// Loop through numbers
// Update max if current number is larger

for (int num : numbers) {
if (num > max) {
max = num;
}
}
return max;
}

Expand All @@ -80,7 +92,9 @@ public static boolean hasDuplicates(Collection<Integer> numbers) {
// TODO:
// Hint:
// Compare the size of a collection with the size of a Set

if (numbers.size() != Set.copyOf(numbers).size()) {
return true;
}
return false;
}

Expand All @@ -96,6 +110,11 @@ public static int countOccurrences(Collection<Integer> numbers, int target) {
// TODO:
// Loop through numbers
// If number equals target, increase count
for (int num : numbers) {
if (num == target) {
count++;
}
}

return count;
}
Expand All @@ -113,6 +132,11 @@ public static Collection<Integer> filterGreaterThanTwenty(Collection<Integer> nu
// TODO:
// Loop through numbers
// Add numbers greater than 20 to result
for (int num : numbers) {
if (num > 20) {
result.add(num);
}
}

return result;
}
Expand Down
Loading