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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# java-collections
# java-collections
# Java Collections Framework Learning Guide

This repository is designed to help you learn the **Java Collections Framework step-by-step**. Each section introduces a new concept, provides examples, and gives you practice problems to reinforce what you learned.
Expand Down
42 changes: 29 additions & 13 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 @@ -19,6 +20,8 @@ public static void main(String[] args) {
System.out.println("Count of even numbers: " + countEven(numbers));
System.out.println("Largest number: " + findMax(numbers));
System.out.println("Contains duplicates? " + hasDuplicates(numbers));
System.out.println("Count occurrences of 20 " + countOccurrences(numbers,20));
System.out.println("Numbers greater than 20: " + filterGreaterThanTwenty(numbers));
}


Expand All @@ -31,8 +34,9 @@ public static int sum(Collection<Integer> numbers) {
int total = 0;

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

return total;
}
Expand All @@ -47,8 +51,11 @@ public static int countEven(Collection<Integer> numbers) {
int count = 0;

// 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 @@ -63,8 +70,11 @@ public static int findMax(Collection<Integer> numbers) {
int max = Integer.MIN_VALUE;

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

return max;
}
Expand All @@ -78,10 +88,9 @@ public static int findMax(Collection<Integer> numbers) {
public static boolean hasDuplicates(Collection<Integer> numbers) {

// TODO:
// Hint:
// Compare the size of a collection with the size of a Set
HashSet<Integer> set = new HashSet<>(numbers);

return false;
return set.size() != numbers.size();
}


Expand All @@ -94,8 +103,12 @@ public static int countOccurrences(Collection<Integer> numbers, int target) {
int count = 0;

// TODO:
// Loop through numbers
// If number equals target, increase count
for (int num : numbers){

if (num == target ){
count++;
}
}

return count;
}
Expand All @@ -111,8 +124,11 @@ 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 (int num : numbers){
if (num >20){
result.add(num);
}
}

return result;
}
Expand Down
47 changes: 24 additions & 23 deletions src/main/java/Collections/Practice/CommonMethodsDemo.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,37 +125,38 @@ public static void main(String[] args) {
// TODO Exploration Section
// ------------------------------

/*
TODO 1:
Create a new Collection called numbers
Add the following values:
10, 20, 30, 40, 50
*/

//TODO 1:
Collection<Integer> numbers = new ArrayList<>();

/*
TODO 2:
Print the size of the numbers collection
*/
numbers.add(10);
numbers.add(20);
numbers.add(30);
numbers.add(40);
numbers.add(50);


/*
TODO 3:
Check if the collection contains 30
*/
//TODO 2:
System.out.println(numbers.size());


/*
TODO 4:
Remove the number 20
*/
//TODO 3:
System.out.println(numbers.contains(30));


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


//TODO 4:
numbers.remove(20);
System.out.println(numbers);



// TODO 5:

for(int num : numbers){
System.out.println(num);
}


/*
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/Collections/Quiz/KnowledgeCheck.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ B. `Collection` extends `List`
C. `Collection` is the root interface for most collection types
D. `Collection` only stores key-value pairs

AW: C

---

## Question 2
Expand All @@ -37,6 +39,8 @@ B. 1
C. 3
D. 30

AW: C

---

## Question 3
Expand All @@ -48,6 +52,8 @@ B. `exists()`
C. `contains()`
D. `check()`

AW:C

---

## Question 4
Expand All @@ -70,6 +76,8 @@ B. `[Jordan]`
C. `[Alex]`
D. `[]`

AW: B

---

## Question 5
Expand All @@ -81,6 +89,8 @@ B. `addAll()`
C. `insertAll()`
D. `merge()`

AW:B

---

## Question 6
Expand All @@ -98,6 +108,8 @@ B. `false`
C. `0`
D. Compilation error

AW:A

---

## Question 7
Expand All @@ -109,6 +121,8 @@ B. `deleteAll()`
C. `clear()`
D. `reset()`

AW:A

---

## Question 8
Expand All @@ -132,6 +146,8 @@ B. `15 10 5`
C. `0 1 2`
D. Compilation error

AW:A

---

## Question 9
Expand All @@ -143,6 +159,8 @@ B. Elements can be removed
C. Indexed access using `.get(index)`
D. Elements can be iterated using a loop

AW:C

---

## Question 10
Expand All @@ -158,5 +176,7 @@ B. It prevents duplicates
C. It allows flexibility to change implementations later
D. It forces the collection to be synchronized

AW:C

```
```
2 changes: 1 addition & 1 deletion src/main/java/Collections/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
```markdown
```markdown
# Collection

## What Is Collection?
Expand Down
Loading