Skip to content
Merged
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
119 changes: 119 additions & 0 deletions src/main/java/Collections/Practice/CollectionBasics.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package Collections.Practice;

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

public class CollectionBasics {
public static void main(String[] args) {

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

numbers.add(4);
numbers.add(8);
numbers.add(15);
numbers.add(16);
numbers.add(23);
numbers.add(42);

System.out.println("Sum of numbers: " + sum(numbers));
System.out.println("Count of even numbers: " + countEven(numbers));
System.out.println("Largest number: " + findMax(numbers));
System.out.println("Contains duplicates? " + hasDuplicates(numbers));
}


/*
PROBLEM 1
Return the sum of all numbers in the collection
*/
public static int sum(Collection<Integer> numbers) {

int total = 0;

// TODO:
// Loop through the collection
// Add each number to total

return total;
}


/*
PROBLEM 2
Count how many numbers are even
*/
public static int countEven(Collection<Integer> numbers) {

int count = 0;

// TODO:
// Loop through the collection
// If the number is even, increase count

return count;
}


/*
PROBLEM 3
Find the largest number in the collection
*/
public static int findMax(Collection<Integer> numbers) {

int max = Integer.MIN_VALUE;

// TODO:
// Loop through numbers
// Update max if current number is larger

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 false;
}


/*
PROBLEM 5
Count how many times a target value appears
*/
public static int countOccurrences(Collection<Integer> numbers, int target) {

int count = 0;

// TODO:
// Loop through numbers
// If number equals target, increase count

return count;
}


/*
BONUS PROBLEM
Create and return a new collection
that only contains numbers greater than 20
*/
public static Collection<Integer> filterGreaterThanTwenty(Collection<Integer> numbers) {

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

// TODO:
// Loop through numbers
// Add numbers greater than 20 to result

return result;
}
}
170 changes: 170 additions & 0 deletions src/main/java/Collections/Practice/CommonMethodsDemo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
package Collections.Practice;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class CommonMethodsDemo {
public static void main(String[] args) {

System.out.println("=== COLLECTION COMMON METHODS DEMO ===\n");

/*
IMPORTANT CONCEPT:
We are using the Collection interface as the reference type.
The object type is ArrayList.
*/

Collection<String> fruits = new ArrayList<>();

// ------------------------------
// add()
// ------------------------------

System.out.println("Adding elements using add()");

fruits.add("Apple");
fruits.add("Banana");
fruits.add("Orange");
fruits.add("Mango");

System.out.println("Fruits: " + fruits);


// ------------------------------
// size()
// ------------------------------

System.out.println("\nChecking size of collection");

int size = fruits.size();
System.out.println("Size: " + size);


// ------------------------------
// contains()
// ------------------------------

System.out.println("\nChecking if collection contains certain items");

boolean hasApple = fruits.contains("Apple");
boolean hasGrape = fruits.contains("Grape");

System.out.println("Contains Apple? " + hasApple);
System.out.println("Contains Grape? " + hasGrape);


// ------------------------------
// remove()
// ------------------------------

System.out.println("\nRemoving an element");

fruits.remove("Banana");

System.out.println("After removing Banana:");
System.out.println(fruits);


// ------------------------------
// addAll()
// ------------------------------

System.out.println("\nAdding multiple elements using addAll()");

List<String> moreFruits = new ArrayList<>();
moreFruits.add("Pineapple");
moreFruits.add("Strawberry");
moreFruits.add("Peach");

fruits.addAll(moreFruits);

System.out.println("After addAll:");
System.out.println(fruits);


// ------------------------------
// Iterating through Collection
// ------------------------------

System.out.println("\nLooping through collection:");

for (String fruit : fruits) {
System.out.println(fruit);
}


// ------------------------------
// isEmpty()
// ------------------------------

System.out.println("\nChecking if collection is empty");

boolean empty = fruits.isEmpty();
System.out.println("Is empty? " + empty);


// ------------------------------
// clear()
// ------------------------------

System.out.println("\nClearing the collection");

fruits.clear();

System.out.println("After clear:");
System.out.println(fruits);


System.out.println("\nCheck if empty after clear:");

System.out.println("Is empty? " + fruits.isEmpty());


// ------------------------------
// TODO Exploration Section
// ------------------------------

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


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


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


/*
TODO 4:
Remove the number 20
*/


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


/*
REFLECTION QUESTIONS:

1. Why can we use Collection as the reference type?
2. What methods are available because of the Collection interface?
3. What methods are NOT available when using Collection instead of List?
*/
}

}
Loading