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: 31 additions & 2 deletions 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 number : numbers){
total+= number;
}

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 number : numbers){
if(number % 2 == 0){
count ++;
}
}

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

return max;
}
Expand All @@ -80,24 +91,42 @@ public static boolean hasDuplicates(Collection<Integer> numbers) {
// TODO:
// Hint:
// Compare the size of a collection with the size of a Set
for (Integer a : numbers) {
int count = 0;

for (Integer b : numbers) {
if (a.equals(b)) {
count++;
}

if (count > 1) {
return true;
}
}
}

return false;
}


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

int count = 0;

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

return count;

}


Expand Down
32 changes: 31 additions & 1 deletion src/main/java/Collections/Practice/CommonMethodsDemo.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,40 +131,70 @@ public static void main(String[] args) {
Add the following values:
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);
System.out.println(numbers);


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

System.out.println("Size collection: "+ numbers.size());

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

for (Integer number: numbers){
if(numbers.contains(30)){
System.out.println(true);
break;
}
}

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


/*
TODO 5:
Loop through the numbers collection
and print each value
*/
for(Integer number: numbers){
System.out.println(number);
}


/*
REFLECTION QUESTIONS:

1. Why can we use Collection as the reference type?

- Interface Parent class of data structures such as lists, sets,
and queues.

2. What methods are available because of the Collection interface?

- add(), remove(), size(), isEmpty();, contains(), iterator(), clear()

3. What methods are NOT available when using Collection instead of List?

- get(int index), set(int index, element), add(int index, element),
remove(int index)

*/

}

}
Loading