From 06a74906ddf07c389d3ad35cad2d0c65004bc3f6 Mon Sep 17 00:00:00 2001 From: mkbrown261 Date: Fri, 6 Mar 2026 16:34:58 -0500 Subject: [PATCH] collectionsframework completed --- .../Practice/CollectionBasics.java | 51 ++++- .../Practice/CommonMethodsDemo.java | 22 +- .../CollectionsHackerrankProblems.java | 191 +++++++++++------- .../Iterable/Practice/IterableWarmups.java | 20 +- .../Lists/ArrayLists/ArrayListProblems.java | 45 ++++- .../Lists/LinkedLists/LinkedListProblems.java | 12 +- .../java/Maps/HashMap/HashMapProblems.java | 19 +- .../LinkedHashMap/LinkedHashMapProblems.java | 19 +- .../java/Maps/TreeMap/TreeMapProblems.java | 10 +- .../Queues/ArrayDeque/ArrayDequeProblems.java | 12 +- src/main/java/Queues/Deque/DequeProblems.java | 16 +- .../java/Sets/HashSet/HashSetProblems.java | 19 +- 12 files changed, 307 insertions(+), 129 deletions(-) diff --git a/src/main/java/Collections/Practice/CollectionBasics.java b/src/main/java/Collections/Practice/CollectionBasics.java index e45cb49..733f8d7 100644 --- a/src/main/java/Collections/Practice/CollectionBasics.java +++ b/src/main/java/Collections/Practice/CollectionBasics.java @@ -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 numbers = new ArrayList<>(); - + filterGreaterThanTwenty(numbers); + numbers.add(4); numbers.add(4); - numbers.add(8); numbers.add(15); numbers.add(16); numbers.add(23); @@ -29,6 +31,10 @@ public static void main(String[] args) { public static int sum(Collection numbers) { int total = 0; + for (int i: numbers){ + total+=i; + } + // TODO: // Loop through the collection @@ -45,6 +51,12 @@ public static int sum(Collection numbers) { public static int countEven(Collection 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 @@ -61,6 +73,13 @@ public static int countEven(Collection numbers) { public static int findMax(Collection numbers) { int max = Integer.MIN_VALUE; + for (int num: numbers){ + if (max < num){ + max= num; + }else { + max = Integer.MIN_VALUE; + } + } // TODO: // Loop through numbers @@ -80,6 +99,22 @@ public static boolean hasDuplicates(Collection numbers) { // TODO: // Hint: // Compare the size of a collection with the size of a Set + Set 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; } @@ -96,6 +131,9 @@ public static int countOccurrences(Collection numbers, int target) { // TODO: // Loop through numbers // If number equals target, increase count + for (int i=0; i==target; ++i){ + ++count; + } return count; } @@ -109,11 +147,16 @@ public static int countOccurrences(Collection numbers, int target) { public static Collection filterGreaterThanTwenty(Collection numbers) { Collection 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; + } } diff --git a/src/main/java/Collections/Practice/CommonMethodsDemo.java b/src/main/java/Collections/Practice/CommonMethodsDemo.java index b2dd6f9..a070b79 100644 --- a/src/main/java/Collections/Practice/CommonMethodsDemo.java +++ b/src/main/java/Collections/Practice/CommonMethodsDemo.java @@ -131,31 +131,41 @@ public static void main(String[] args) { Add the following values: 10, 20, 30, 40, 50 */ - +Collection 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); + } /* diff --git a/src/main/java/CollectionsHackerrank/CollectionsHackerrankProblems.java b/src/main/java/CollectionsHackerrank/CollectionsHackerrankProblems.java index 6c2f4e1..deaa3f2 100644 --- a/src/main/java/CollectionsHackerrank/CollectionsHackerrankProblems.java +++ b/src/main/java/CollectionsHackerrank/CollectionsHackerrankProblems.java @@ -1,8 +1,6 @@ package CollectionsHackerrank; -import java.util.List; -import java.util.Map; -import java.util.Queue; +import java.util.*; public class CollectionsHackerrankProblems { public class CollectionsHackerrankPractice { @@ -16,44 +14,47 @@ public static void main(String[] args) { /* Problem 1 Remove duplicates from a list of integers. - - Example - Input: [1,2,2,3,4,4,5] - Output: [1,2,3,4,5] */ public static List removeDuplicates(List numbers) { - // TODO: Implement this method - - return null; + Set set = new LinkedHashSet<>(numbers); + return new ArrayList<>(set); } /* Problem 2 Count how many times each number appears. - - Example - Input: [1,2,2,3,3,3] - Output: {1=1, 2=2, 3=3} */ public static Map countFrequency(List numbers) { - // TODO: Implement this method + Map map = new HashMap<>(); - return null; + for(Integer num : numbers){ + + map.put(num, map.getOrDefault(num, 0) + 1); + + } + + return map; } /* Problem 3 Return the first number that appears only once. - - Example - Input: [4,5,1,2,0,4] - Output: 5 */ public static Integer firstUnique(List numbers) { - // TODO: Implement this method + Map map = new HashMap<>(); + + for(Integer num : numbers){ + map.put(num, map.getOrDefault(num, 0) + 1); + } + + for(Integer num : numbers){ + if(map.get(num) == 1){ + return num; + } + } return null; } @@ -61,16 +62,22 @@ public static Integer firstUnique(List numbers) { /* Problem 4 Return true if any two numbers add up to the target. - - Example - numbers = [2,7,11,15] - target = 9 - - Output: true */ public static boolean twoSum(List numbers, int target) { - // TODO: Implement this method + Set set = new HashSet<>(); + + for(Integer num : numbers){ + + int complement = target - num; + + if(set.contains(complement)){ + return true; + } + + set.add(num); + + } return false; } @@ -78,101 +85,135 @@ public static boolean twoSum(List numbers, int target) { /* Problem 5 Count how many unique words exist in a list. - - Example - Input: ["apple","banana","apple","orange"] - Output: 3 */ public static int countUniqueWords(List words) { - // TODO: Implement this method + Set set = new HashSet<>(words); - return 0; + return set.size(); } /* Problem 6 Reverse a queue. - - Example - Input: [1,2,3,4] - Output: [4,3,2,1] */ public static Queue reverseQueue(Queue queue) { - // TODO: Implement this method + Stack stack = new Stack<>(); - return null; + while(!queue.isEmpty()){ + stack.push(queue.poll()); + } + + while(!stack.isEmpty()){ + queue.add(stack.pop()); + } + + return queue; } /* Problem 7 Determine whether parentheses are balanced. - - Example - Input: "(())" - Output: true - - Input: "(()" - Output: false */ public static boolean isBalanced(String expression) { - // TODO: Implement this method + Stack stack = new Stack<>(); - return false; + for(char c : expression.toCharArray()){ + + if(c == '('){ + stack.push(c); + } + else if(c == ')'){ + + if(stack.isEmpty()){ + return false; + } + + stack.pop(); + + } + + } + + return stack.isEmpty(); } /* Problem 8 Return the number that appears most frequently in the list. - - Example - Input: [1,3,2,3,4,3] - Output: 3 */ public static Integer mostFrequent(List numbers) { - // TODO: Implement this method + Map map = new HashMap<>(); - return null; + for(Integer num : numbers){ + map.put(num, map.getOrDefault(num, 0) + 1); + } + + int maxCount = 0; + Integer result = null; + + for(Map.Entry entry : map.entrySet()){ + + if(entry.getValue() > maxCount){ + + maxCount = entry.getValue(); + result = entry.getKey(); + + } + + } + + return result; } /* Problem 9 Group words based on their length. - - Example - Input: ["cat","dog","elephant","ant"] - - Output: - { - 3 = ["cat","dog","ant"], - 8 = ["elephant"] - } */ public static Map> groupByLength(List words) { - // TODO: Implement this method + Map> map = new HashMap<>(); - return null; + for(String word : words){ + + int length = word.length(); + + map.putIfAbsent(length, new ArrayList<>()); + + map.get(length).add(word); + + } + + return map; } /* Problem 10 Return the maximum sum of any window of size k. - - Example - numbers = [2,1,5,1,3,2] - k = 3 - - Output: 9 */ public static int maxSlidingWindowSum(List numbers, int k) { - // TODO: Implement this method + int maxSum = 0; + int windowSum = 0; + + for(int i = 0; i < numbers.size(); i++){ + + windowSum += numbers.get(i); + + if(i >= k - 1){ + + maxSum = Math.max(maxSum, windowSum); + + windowSum -= numbers.get(i - (k - 1)); + + } + + } - return 0; + return maxSum; } } -} +} \ No newline at end of file diff --git a/src/main/java/Iterable/Practice/IterableWarmups.java b/src/main/java/Iterable/Practice/IterableWarmups.java index 9e9de94..fc0be49 100644 --- a/src/main/java/Iterable/Practice/IterableWarmups.java +++ b/src/main/java/Iterable/Practice/IterableWarmups.java @@ -30,6 +30,9 @@ public static int sum(Iterable numbers) { // TODO: // Use a for-each loop to calculate the sum + for (int num : numbers) { + total += num; + } return total; } @@ -46,6 +49,11 @@ public static int countEven(Iterable numbers) { // TODO: // Loop through numbers // Increment count if number is even + for (int num : numbers) { + if (num % 2 == 0) { + count++; + } + } return count; } @@ -62,6 +70,11 @@ public static int findMax(Iterable numbers) { // TODO: // Loop through numbers // Update max if current number is larger + for (int num : numbers) { + if (num > max) { + max = num; + } + } return max; } @@ -78,7 +91,12 @@ public static int countMatches(Iterable words, String target) { // TODO: // Loop through words // Compare each word to target + for (String word : words) { + if (word.equals(target)) { + count++; + } + } return count; } -} +} \ No newline at end of file diff --git a/src/main/java/Lists/ArrayLists/ArrayListProblems.java b/src/main/java/Lists/ArrayLists/ArrayListProblems.java index baf4dfa..bf8abce 100644 --- a/src/main/java/Lists/ArrayLists/ArrayListProblems.java +++ b/src/main/java/Lists/ArrayLists/ArrayListProblems.java @@ -34,8 +34,13 @@ public static void main(String[] args) { public static int sum(List nums) { // TODO: Implement this method + int total = 0; - return 0; + for (int num : nums) { + total += num; + } + + return total; } /* @@ -49,8 +54,15 @@ public static int sum(List nums) { public static int countEvens(List nums) { // TODO: Implement this method + int count = 0; + + for (int num : nums) { + if (num % 2 == 0) { + count++; + } + } - return 0; + return count; } /* @@ -67,6 +79,17 @@ public static int countEvens(List nums) { public static boolean hasDuplicate(List nums) { // TODO: Implement this method + for (int i = 0; i < nums.size(); i++) { + + for (int j = i + 1; j < nums.size(); j++) { + + if (nums.get(i).equals(nums.get(j))) { + return true; + } + + } + + } return false; } @@ -82,8 +105,15 @@ public static boolean hasDuplicate(List nums) { public static int findMax(List nums) { // TODO: Implement this method + int max = Integer.MIN_VALUE; - return 0; + for (int num : nums) { + if (num > max) { + max = num; + } + } + + return max; } /* @@ -99,7 +129,12 @@ public static int findMax(List nums) { public static List reverse(List nums) { // TODO: Implement this method + List reversed = new ArrayList<>(); + + for (int i = nums.size() - 1; i >= 0; i--) { + reversed.add(nums.get(i)); + } - return null; + return reversed; } -} +} \ No newline at end of file diff --git a/src/main/java/Lists/LinkedLists/LinkedListProblems.java b/src/main/java/Lists/LinkedLists/LinkedListProblems.java index 178a2ba..4498ab8 100644 --- a/src/main/java/Lists/LinkedLists/LinkedListProblems.java +++ b/src/main/java/Lists/LinkedLists/LinkedListProblems.java @@ -38,6 +38,7 @@ public static void main(String[] args) { public static void addToFront(LinkedList list, int value) { // TODO: Implement this method + list.addFirst(value); } @@ -52,6 +53,7 @@ public static void addToFront(LinkedList list, int value) { public static void addToEnd(LinkedList list, int value) { // TODO: Implement this method + list.addLast(value); } @@ -66,6 +68,7 @@ public static void addToEnd(LinkedList list, int value) { public static void removeFirstElement(LinkedList list) { // TODO: Implement this method + list.removeFirst(); } @@ -80,6 +83,7 @@ public static void removeFirstElement(LinkedList list) { public static void removeLastElement(LinkedList list) { // TODO: Implement this method + list.removeLast(); } @@ -94,8 +98,7 @@ public static void removeLastElement(LinkedList list) { public static int getFirstElement(LinkedList list) { // TODO: Implement this method - - return 0; + return list.getFirst(); } /* @@ -109,7 +112,6 @@ public static int getFirstElement(LinkedList list) { public static int getLastElement(LinkedList list) { // TODO: Implement this method - - return 0; + return list.getLast(); } -} +} \ No newline at end of file diff --git a/src/main/java/Maps/HashMap/HashMapProblems.java b/src/main/java/Maps/HashMap/HashMapProblems.java index 04b5567..f2ac064 100644 --- a/src/main/java/Maps/HashMap/HashMapProblems.java +++ b/src/main/java/Maps/HashMap/HashMapProblems.java @@ -35,6 +35,7 @@ public static void main(String[] args) { public static void addItem(Map map, String item, int quantity) { // TODO: Implement this method + map.put(item, quantity); } @@ -49,8 +50,7 @@ public static void addItem(Map map, String item, int quantity) public static int getQuantity(Map map, String item) { // TODO: Implement this method - - return 0; + return map.get(item); } /* @@ -64,6 +64,7 @@ public static int getQuantity(Map map, String item) { public static void updateQuantity(Map map, String item, int newQuantity) { // TODO: Implement this method + map.put(item, newQuantity); } @@ -78,6 +79,7 @@ public static void updateQuantity(Map map, String item, int new public static void removeItem(Map map, String item) { // TODO: Implement this method + map.remove(item); } @@ -92,7 +94,16 @@ public static void removeItem(Map map, String item) { public static Map countFrequency(List numbers) { // TODO: Implement this method + Map frequency = new HashMap<>(); + + for (int num : numbers) { + if (frequency.containsKey(num)) { + frequency.put(num, frequency.get(num) + 1); + } else { + frequency.put(num, 1); + } + } - return null; + return frequency; } -} +} \ No newline at end of file diff --git a/src/main/java/Maps/LinkedHashMap/LinkedHashMapProblems.java b/src/main/java/Maps/LinkedHashMap/LinkedHashMapProblems.java index e8bbdc2..90f883d 100644 --- a/src/main/java/Maps/LinkedHashMap/LinkedHashMapProblems.java +++ b/src/main/java/Maps/LinkedHashMap/LinkedHashMapProblems.java @@ -33,6 +33,7 @@ public static void main(String[] args) { public static void addStudent(Map map, String name, int grade) { // TODO: Implement this method + map.put(name, grade); } @@ -47,6 +48,7 @@ public static void addStudent(Map map, String name, int grade) public static void updateGrade(Map map, String name, int newGrade) { // TODO: Implement this method + map.put(name, newGrade); } @@ -61,6 +63,7 @@ public static void updateGrade(Map map, String name, int newGra public static void removeStudent(Map map, String name) { // TODO: Implement this method + map.remove(name); } @@ -75,6 +78,9 @@ public static void removeStudent(Map map, String name) { public static String getFirstInserted(Map map) { // TODO: Implement this method + for (String key : map.keySet()) { + return key; + } return null; } @@ -91,7 +97,16 @@ public static String getFirstInserted(Map map) { public static Map wordFrequency(List words) { // TODO: Implement this method + Map frequency = new LinkedHashMap<>(); - return null; + for (String word : words) { + if (frequency.containsKey(word)) { + frequency.put(word, frequency.get(word) + 1); + } else { + frequency.put(word, 1); + } + } + + return frequency; } -} +} \ No newline at end of file diff --git a/src/main/java/Maps/TreeMap/TreeMapProblems.java b/src/main/java/Maps/TreeMap/TreeMapProblems.java index b101d2e..d704a99 100644 --- a/src/main/java/Maps/TreeMap/TreeMapProblems.java +++ b/src/main/java/Maps/TreeMap/TreeMapProblems.java @@ -31,6 +31,7 @@ public static void main(String[] args) { public static void addPlayer(TreeMap map, int rank, String name) { // TODO: Implement this method + map.put(rank, name); } @@ -45,8 +46,8 @@ Return the player with the highest ranking (smallest key). public static String getTopPlayer(TreeMap map) { // TODO: Implement this method + return map.get(map.firstKey()); - return null; } /* @@ -60,8 +61,8 @@ Return the player with the lowest ranking (largest key). public static String getLowestPlayer(TreeMap map) { // TODO: Implement this method + return map.get(map.lastKey()); - return null; } /* @@ -75,6 +76,7 @@ public static String getLowestPlayer(TreeMap map) { public static void removePlayer(TreeMap map, int rank) { // TODO: Implement this method + map.remove(rank); } @@ -89,7 +91,7 @@ public static void removePlayer(TreeMap map, int rank) { public static Integer getNextRank(TreeMap map, int rank) { // TODO: Implement this method + return map.higherKey(rank); - return null; } -} +} \ No newline at end of file diff --git a/src/main/java/Queues/ArrayDeque/ArrayDequeProblems.java b/src/main/java/Queues/ArrayDeque/ArrayDequeProblems.java index dac03cf..830678e 100644 --- a/src/main/java/Queues/ArrayDeque/ArrayDequeProblems.java +++ b/src/main/java/Queues/ArrayDeque/ArrayDequeProblems.java @@ -31,6 +31,7 @@ public static void main(String[] args) { public static void addToFront(ArrayDeque deque, int value) { // TODO: Implement this method + deque.addFirst(value); } @@ -45,6 +46,7 @@ public static void addToFront(ArrayDeque deque, int value) { public static void addToBack(ArrayDeque deque, int value) { // TODO: Implement this method + deque.addLast(value); } @@ -59,6 +61,7 @@ public static void addToBack(ArrayDeque deque, int value) { public static void removeFront(ArrayDeque deque) { // TODO: Implement this method + deque.removeFirst(); } @@ -73,6 +76,7 @@ public static void removeFront(ArrayDeque deque) { public static void removeBack(ArrayDeque deque) { // TODO: Implement this method + deque.removeLast(); } @@ -87,8 +91,7 @@ public static void removeBack(ArrayDeque deque) { public static Integer peekFront(ArrayDeque deque) { // TODO: Implement this method - - return null; + return deque.peekFirst(); } /* @@ -102,7 +105,6 @@ public static Integer peekFront(ArrayDeque deque) { public static Integer peekBack(ArrayDeque deque) { // TODO: Implement this method - - return null; + return deque.peekLast(); } -} +} \ No newline at end of file diff --git a/src/main/java/Queues/Deque/DequeProblems.java b/src/main/java/Queues/Deque/DequeProblems.java index 7ef0c06..33b8f40 100644 --- a/src/main/java/Queues/Deque/DequeProblems.java +++ b/src/main/java/Queues/Deque/DequeProblems.java @@ -20,6 +20,7 @@ public static void main(String[] args) { public static void addFront(Deque deque, int value) { // TODO: Implement this method + deque.addFirst(value); } @@ -34,6 +35,7 @@ public static void addFront(Deque deque, int value) { public static void addBack(Deque deque, int value) { // TODO: Implement this method + deque.addLast(value); } @@ -48,8 +50,7 @@ public static void addBack(Deque deque, int value) { public static Integer removeFront(Deque deque) { // TODO: Implement this method - - return null; + return deque.removeFirst(); } /* @@ -63,8 +64,7 @@ public static Integer removeFront(Deque deque) { public static Integer removeBack(Deque deque) { // TODO: Implement this method - - return null; + return deque.removeLast(); } /* @@ -78,8 +78,7 @@ public static Integer removeBack(Deque deque) { public static Integer peekFront(Deque deque) { // TODO: Implement this method - - return null; + return deque.peekFirst(); } /* @@ -93,8 +92,7 @@ public static Integer peekFront(Deque deque) { public static Integer peekBack(Deque deque) { // TODO: Implement this method - - return null; + return deque.peekLast(); } -} +} \ No newline at end of file diff --git a/src/main/java/Sets/HashSet/HashSetProblems.java b/src/main/java/Sets/HashSet/HashSetProblems.java index a36c570..341c795 100644 --- a/src/main/java/Sets/HashSet/HashSetProblems.java +++ b/src/main/java/Sets/HashSet/HashSetProblems.java @@ -2,6 +2,7 @@ import java.util.List; import java.util.Set; +import java.util.HashSet; public class HashSetProblems { public static void main(String[] args) { @@ -20,7 +21,7 @@ public static void main(String[] args) { */ public static void addElement(Set set, String value) { - // TODO: Implement this method + set.add(value); } @@ -34,9 +35,8 @@ public static void addElement(Set set, String value) { */ public static boolean containsValue(Set set, String value) { - // TODO: Implement this method + return set.contains(value); - return false; } /* @@ -49,7 +49,7 @@ public static boolean containsValue(Set set, String value) { */ public static void removeValue(Set set, String value) { - // TODO: Implement this method + set.remove(value); } @@ -63,9 +63,8 @@ public static void removeValue(Set set, String value) { */ public static int getUniqueCount(Set set) { - // TODO: Implement this method + return set.size(); - return 0; } /* @@ -78,8 +77,10 @@ public static int getUniqueCount(Set set) { */ public static Set getUniqueValues(List numbers) { - // TODO: Implement this method + Set uniqueValues = new HashSet<>(); - return null; + uniqueValues.addAll(numbers); + + return uniqueValues; } -} +} \ No newline at end of file