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
51 changes: 47 additions & 4 deletions src/main/java/Collections/Practice/CollectionBasics.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@

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) {

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

filterGreaterThanTwenty(numbers);
numbers.add(4);
numbers.add(4);
numbers.add(8);
numbers.add(15);
numbers.add(16);
numbers.add(23);
Expand All @@ -29,6 +31,10 @@ public static void main(String[] args) {
public static int sum(Collection<Integer> numbers) {

int total = 0;
for (int i: numbers){
total+=i;
}


// TODO:
// Loop through the collection
Expand All @@ -45,6 +51,12 @@ public static int sum(Collection<Integer> numbers) {
public static int countEven(Collection<Integer> numbers) {

int count = 0;
for (int i = 0; i<=numbers.size(); ++i){
if (i %2==0){
count++;
System.out.println(count);
}
}

// TODO:
// Loop through the collection
Expand All @@ -61,6 +73,13 @@ public static int countEven(Collection<Integer> numbers) {
public static int findMax(Collection<Integer> numbers) {

int max = Integer.MIN_VALUE;
for (int num: numbers){
if (max < num){
max= num;
}else {
max = Integer.MIN_VALUE;
}
}

// TODO:
// Loop through numbers
Expand All @@ -80,6 +99,22 @@ public static boolean hasDuplicates(Collection<Integer> numbers) {
// TODO:
// Hint:
// Compare the size of a collection with the size of a Set
Set<Integer> numbersset= new HashSet<>();
numbersset.add(4);
numbersset.add(4);
numbersset.add(15);
numbersset.add(16);
numbersset.add(23);
numbersset.add(42);

for (int i = 0; i< numbers.size();++i){
for (int e = 0; e < numbersset.size(); ++i ){
if (i==e){
return true;
}
}

}

return false;
}
Expand All @@ -96,6 +131,9 @@ public static int countOccurrences(Collection<Integer> numbers, int target) {
// TODO:
// Loop through numbers
// If number equals target, increase count
for (int i=0; i==target; ++i){
++count;
}

return count;
}
Expand All @@ -109,11 +147,16 @@ public static int countOccurrences(Collection<Integer> numbers, int target) {
public static Collection<Integer> filterGreaterThanTwenty(Collection<Integer> numbers) {

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

for (int num:numbers){
if (num>20){
result.add(num);
}
}
// TODO:
// Loop through numbers
// Add numbers greater than 20 to result

System.out.println(result);
return result;

}
}
22 changes: 16 additions & 6 deletions src/main/java/Collections/Practice/CommonMethodsDemo.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,31 +131,41 @@ 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);

/*
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("thirty is here");
}else {
System.out.println("no thirty here");
}
/*
TODO 4:
Remove the number 20
*/

numbers.remove(20);

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


/*
Expand Down
Loading