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
52 changes: 26 additions & 26 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.HashSet;

public class CollectionBasics {
public static void main(String[] args) {
Expand All @@ -21,7 +22,6 @@ public static void main(String[] args) {
System.out.println("Contains duplicates? " + hasDuplicates(numbers));
}


/*
PROBLEM 1
Return the sum of all numbers in the collection
Expand All @@ -30,14 +30,13 @@ public static int sum(Collection<Integer> numbers) {

int total = 0;

// TODO:
// Loop through the collection
// Add each number to total
for (Integer num : numbers) {
total += num;
}

return total;
}


/*
PROBLEM 2
Count how many numbers are even
Expand All @@ -46,14 +45,15 @@ public static int countEven(Collection<Integer> numbers) {

int count = 0;

// TODO:
// Loop through the collection
// If the number is even, increase count
for (Integer num : numbers) {
if (num % 2 == 0) {
count++;
}
}

return count;
}


/*
PROBLEM 3
Find the largest number in the collection
Expand All @@ -62,29 +62,26 @@ public static int findMax(Collection<Integer> numbers) {

int max = Integer.MIN_VALUE;

// TODO:
// Loop through numbers
// Update max if current number is larger
for (Integer num : numbers) {
if (num > max) {
max = num;
}
}

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
return numbers.size() != new HashSet<>(numbers).size();

return false;
}


/*
PROBLEM 5
Count how many times a target value appears
Expand All @@ -93,14 +90,15 @@ public static int countOccurrences(Collection<Integer> numbers, int target) {

int count = 0;

// TODO:
// Loop through numbers
// If number equals target, increase count
for (Integer num : numbers) {
if (num == target) {
count++;
}
}

return count;
}


/*
BONUS PROBLEM
Create and return a new collection
Expand All @@ -110,10 +108,12 @@ public static Collection<Integer> filterGreaterThanTwenty(Collection<Integer> nu

Collection<Integer> result = new ArrayList<>();

// TODO:
// Loop through numbers
// Add numbers greater than 20 to result
for (Integer num : numbers) {
if (num > 20) {
result.add(num);
}
}

return result;
}
}
}
20 changes: 10 additions & 10 deletions src/main/java/Collections/Quiz/KnowledgeCheck.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ A. `Collection` is a class
B. `Collection` extends `List`
C. `Collection` is the root interface for most collection types
D. `Collection` only stores key-value pairs

C
---

## Question 2
Expand All @@ -36,7 +36,7 @@ A. 0
B. 1
C. 3
D. 30

C
---

## Question 3
Expand All @@ -47,7 +47,7 @@ A. `search()`
B. `exists()`
C. `contains()`
D. `check()`

C
---

## Question 4
Expand All @@ -69,7 +69,7 @@ A. `[Alex, Jordan]`
B. `[Jordan]`
C. `[Alex]`
D. `[]`

B
---

## Question 5
Expand All @@ -80,7 +80,7 @@ A. `add()`
B. `addAll()`
C. `insertAll()`
D. `merge()`

B
---

## Question 6
Expand All @@ -97,7 +97,7 @@ A. `true`
B. `false`
C. `0`
D. Compilation error

A
---

## Question 7
Expand All @@ -108,7 +108,7 @@ A. `removeAll()`
B. `deleteAll()`
C. `clear()`
D. `reset()`

C
---

## Question 8
Expand All @@ -131,7 +131,7 @@ A. `5 10 15`
B. `15 10 5`
C. `0 1 2`
D. Compilation error

A
---

## Question 9
Expand All @@ -142,7 +142,7 @@ A. Elements can be added
B. Elements can be removed
C. Indexed access using `.get(index)`
D. Elements can be iterated using a loop

C
---

## Question 10
Expand All @@ -157,6 +157,6 @@ A. It automatically sorts the collection
B. It prevents duplicates
C. It allows flexibility to change implementations later
D. It forces the collection to be synchronized

C
```
```
Loading