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: 29 additions & 1 deletion src/main/java/Collections/Practice/CollectionBasics.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ public static int sum(Collection<Integer> numbers) {
// TODO:
// Loop through the collection
// Add each number to total
for(Integer s : numbers){
total +=s;
}

return total;
}
Expand All @@ -49,6 +52,11 @@ public static int countEven(Collection<Integer> numbers) {
// TODO:
// Loop through the collection
// If the number is even, increase count
for(Integer ce : numbers) {
if(ce %2==0){
count++;
}
}

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

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

Set<Interger> see = new HasSet<>();
for(Integer n: see){
if(see.contains(n)){
return true;
}see.add(n);
}
return false;
}

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

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

return result;
}
Expand Down
36 changes: 32 additions & 4 deletions src/main/java/Collections/Practice/CommonMethodsDemo.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,39 +131,67 @@ public static void main(String[] args) {
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()
*/
}

Expand Down
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

Answer : C
---

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

Answer: C
---

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

Answer: C
---

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

Answer: B
---

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

Answer: B
---

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

Answer: A
---

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

Answer: A
---

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

Answer: 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

Answer: 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

Answer: C
```
```
31 changes: 31 additions & 0 deletions src/main/java/Collections/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,41 @@ You can switch to `HashSet` or `LinkedList` later without changing much code.
## Practice Ideas

* Write a method that accepts `Collection<Integer>` and returns the sum
import java.util.Collection;

public static int sumCollection (Collection<Integer> numbers) {
int sum = 0;

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

return sum;
}

* Find intersection of two collections

Collection<Integer> intersection(Collection<Integer> a, Collection<Integer>b){
Collection<Integer> result = new ArrayList<>>();
for(Integer i:a){
if(b.contains(i)){
result.add(i);
}
}
return(result);
}


* Remove duplicates using a Set

public static Collection<Integer> removeDuplicate (Collection<Integer> numbers){
return new HasSet<>(numbers);
}

* Determine if one collection is a subset of another

public static boolean isSubset(Collection<Integer> a, Collection<Integer> b ){
return b.containsAll(a);
---

## Summary
Expand Down
Loading