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
33 changes: 33 additions & 0 deletions src/main/java/Collections/Practice/CollectionBasics.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

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) {
Expand Down Expand Up @@ -34,6 +36,10 @@ public static int sum(Collection<Integer> numbers) {
// Loop through the collection
// Add each number to total

for (Integer number : numbers) {
total += number;
}

return total;
}

Expand All @@ -50,6 +56,12 @@ public static int countEven(Collection<Integer> numbers) {
// Loop through the collection
// If the number is even, increase count

for (Integer number : numbers) {
if (number % 2 == 0) {
count++;
}
}

return count;
}

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

return max;
}
Expand All @@ -80,6 +97,11 @@ public static boolean hasDuplicates(Collection<Integer> numbers) {
// TODO:
// Hint:
// Compare the size of a collection with the size of a Set
Set<Integer> set = new HashSet<>(numbers);
if (set.size() < numbers.size()) {
return true;
}


return false;
}
Expand All @@ -97,6 +119,12 @@ public static int countOccurrences(Collection<Integer> numbers, int target) {
// Loop through numbers
// If number equals target, increase count

for (Integer number : numbers) {
if (number.equals(target)) {
count++;
}
}

return count;
}

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

return result;
}
Expand Down
26 changes: 22 additions & 4 deletions src/main/java/Collections/Practice/CommonMethodsDemo.java
Original file line number Diff line number Diff line change
Expand Up @@ -132,31 +132,45 @@ public static void main(String[] args) {
10, 20, 30, 40, 50
*/

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

numbers.add(10);
numbers.add(20);
numbers.add(30);
numbers.add(40);
numbers.add(50);


/*
TODO 2:
Print the size of the numbers collection
*/

System.out.println(numbers.size());

/*
TODO 3:
Check if the collection contains 30
*/

if(numbers.contains(30)){
System.out.println("True");
}else{
System.out.println("false");
}

/*
TODO 4:
Remove the number 20
*/

numbers.remove(20);

/*
TODO 5:
Loop through the numbers collection
and print each value
*/

for (Integer number : numbers) {
System.out.println(number);
}

/*
REFLECTION QUESTIONS:
Expand All @@ -165,6 +179,10 @@ public static void main(String[] args) {
2. What methods are available because of the Collection interface?
3. What methods are NOT available when using Collection instead of List?
*/

//1. because it is a parent of list, set and queue so we can use it for all objects that are a collection
//2 .add(), .remove(), .size(), .contains(), .isEmpty(), .clear(), .addAll(), .removeAll(), .retainAll()
//3. indexing, i.e. .get()
}

}
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 @@ -14,7 +14,7 @@ Which of the following is **true** about the `Collection` interface?

A. `Collection` is a class
B. `Collection` extends `List`
C. `Collection` is the root interface for most collection types
**C. `Collection` is the root interface for most collection types**
D. `Collection` only stores key-value pairs

---
Expand All @@ -34,7 +34,7 @@ System.out.println(numbers.size());

A. 0
B. 1
C. 3
**C. 3**
D. 30

---
Expand All @@ -45,7 +45,7 @@ Which method is used to **check if an element exists** inside a collection?

A. `search()`
B. `exists()`
C. `contains()`
**C. `contains()`**
D. `check()`

---
Expand All @@ -66,7 +66,7 @@ System.out.println(names);
```

A. `[Alex, Jordan]`
B. `[Jordan]`
**B. `[Jordan]`**
C. `[Alex]`
D. `[]`

Expand All @@ -77,7 +77,7 @@ D. `[]`
Which method adds **all elements from another collection** into an existing collection?

A. `add()`
B. `addAll()`
**B. `addAll()`**
C. `insertAll()`
D. `merge()`

Expand All @@ -93,7 +93,7 @@ Collection<Integer> numbers = new ArrayList<>();
System.out.println(numbers.isEmpty());
```

A. `true`
**A. `true`**
B. `false`
C. `0`
D. Compilation error
Expand All @@ -104,7 +104,7 @@ D. Compilation error

Which method removes **all elements** from a collection?

A. `removeAll()`
**A. `removeAll()`**
B. `deleteAll()`
C. `clear()`
D. `reset()`
Expand All @@ -127,7 +127,7 @@ for(Integer n : nums) {
}
```

A. `5 10 15`
**A. `5 10 15`**
B. `15 10 5`
C. `0 1 2`
D. Compilation error
Expand All @@ -140,7 +140,7 @@ Which of the following **cannot be guaranteed** when using the `Collection` inte

A. Elements can be added
B. Elements can be removed
C. Indexed access using `.get(index)`
**C. Indexed access using `.get(index)`**
D. Elements can be iterated using a loop

---
Expand All @@ -155,7 +155,7 @@ Collection<String> fruits = new ArrayList<>();

A. It automatically sorts the collection
B. It prevents duplicates
C. It allows flexibility to change implementations later
**C. It allows flexibility to change implementations later**
D. It forces the collection to be synchronized

```
Expand Down
Loading