diff --git a/src/main/java/Collections/CollectionsDemo.java b/src/main/java/Collections/CollectionsDemo.java new file mode 100644 index 0000000..b70f25c --- /dev/null +++ b/src/main/java/Collections/CollectionsDemo.java @@ -0,0 +1,32 @@ +package Collections; + +import java.util.ArrayList; +import java.util.Collection; + +public class CollectionsDemo { + public static void run() { + System.out.println("=== Collection Demo ==="); + + Collection fruits = new ArrayList<>(); + + fruits.add("Apple"); + + fruits.add("Banana"); + + fruits.add("Orange"); + + System.out.println("Starting collection: " + fruits); + + System.out.println("Size: " + fruits.size()); + + System.out.println("Contains Banana? " + fruits.contains("Banana")); + + fruits.remove("Apple"); + + System.out.println("After removing Apple: " + fruits); + + System.out.println("Is empty? " + fruits.isEmpty()); + + System.out.println(); + } +} \ No newline at end of file diff --git a/src/main/java/CollectionsHackerrank/CollectionsHackerrankDemo.java b/src/main/java/CollectionsHackerrank/CollectionsHackerrankDemo.java new file mode 100644 index 0000000..a27d623 --- /dev/null +++ b/src/main/java/CollectionsHackerrank/CollectionsHackerrankDemo.java @@ -0,0 +1,151 @@ +package CollectionsHackerrank; + +import java.util.*; + +public class CollectionsHackerrankDemo { + public static void run() { + System.out.println("=== Collections HackerRank Practice ==="); + + problem1_removeDuplicates(); + problem2_countWordFrequency(); + problem3_reverseList(); + problem4_sortWithTreeSet(); + problem5_anagramCheck(); + problem6_firstNonRepeatingCharacter(); + problem7_queueSimulation(); + problem8_stackExample(); + problem9_studentGradesMap(); + problem10_priorityQueueExample(); + } + + public static void problem1_removeDuplicates() { + List numbers = Arrays.asList(1, 2, 2, 3, 4, 4, 5); + Set unique = new LinkedHashSet<>(numbers); + System.out.println("1. Remove duplicates: " + unique); + } + + public static void problem2_countWordFrequency() { + String sentence = "apple banana apple orange banana apple"; + String[] words = sentence.split(" "); + Map frequency = new HashMap<>(); + + for (String word : words) { + frequency.put(word, frequency.getOrDefault(word, 0) + 1); + } + + System.out.println("2. Word frequency: " + frequency); + } + + public static void problem3_reverseList() { + + List items = new ArrayList<>(Arrays.asList("A", "B", "C", "D")); + + Collections.reverse(items); + + System.out.println("3. Reversed list: " + items); + + } + + public static void problem4_sortWithTreeSet() { + + Set numbers = new TreeSet<>(Arrays.asList(50, 10, 40, 20, 10, 30)); + + System.out.println("4. Sorted unique numbers: " + numbers); + } + + public static void problem5_anagramCheck() { + + String a = "listen"; + + String b = "silent"; + + char[] arr1 = a.toCharArray(); + + char[] arr2 = b.toCharArray(); + + Arrays.sort(arr1); + + Arrays.sort(arr2); + + boolean isAnagram = Arrays.equals(arr1, arr2); + + System.out.println("5. Are listen and silent anagrams? " + isAnagram); + } + + public static void problem6_firstNonRepeatingCharacter() { + + String word = "swiss"; + + Map frequency = new LinkedHashMap<>(); + + for (char c : word.toCharArray()) { + + frequency.put(c, frequency.getOrDefault(c, 0) + 1); + } + + Character firstUnique = null; + + for (Map.Entry entry : frequency.entrySet()) { + + if (entry.getValue() == 1) { + firstUnique = entry.getKey(); + break; + } + } + + System.out.println("6. First non-repeating character in 'swiss': " + firstUnique); + } + + public static void problem7_queueSimulation() { + + Queue customers = new LinkedList<>(); + + customers.offer("Customer1"); + + customers.offer("Customer2"); + + customers.offer("Customer3"); + + System.out.println("7. Serving: " + customers.poll()); + + System.out.println(" Remaining queue: " + customers); + } + + public static void problem8_stackExample() { + + Stack stack = new Stack<>(); + + stack.push("("); + + stack.push("{"); + + stack.pop(); + + System.out.println("8. Stack contents: " + stack); + } + + public static void problem9_studentGradesMap() { + Map grades = new HashMap<>(); + + grades.put("Kenneth", 95); + + grades.put("Jordan", 88); + + grades.put("Chris", 91); + + System.out.println("9. Student grades: " + grades); + } + + public static void problem10_priorityQueueExample() { + PriorityQueue pq = new PriorityQueue<>(); + pq.offer(25); + + pq.offer(5); + + pq.offer(15); + + pq.offer(1); + + System.out.println("10. Smallest item from PriorityQueue: " + pq.poll()); + } +} \ No newline at end of file diff --git a/src/main/java/Hierachy/HierarchyDemo.java b/src/main/java/Hierachy/HierarchyDemo.java new file mode 100644 index 0000000..bc7653e --- /dev/null +++ b/src/main/java/Hierachy/HierarchyDemo.java @@ -0,0 +1,20 @@ +package Hierarchy; + +public class HierarchyDemo { + public static void run() { + System.out.println("=== Hierarchy Demo ==="); + + System.out.println("Iterable"); + + System.out.println(" -> Collection"); + + System.out.println(" -> List"); + + System.out.println(" -> Set"); + + System.out.println(" -> Queue"); + + System.out.println("Map belongs to the Collections Framework, but does not extend Collection."); + System.out.println(); + } +} \ No newline at end of file diff --git a/src/main/java/Iterable/IterableDemo.java b/src/main/java/Iterable/IterableDemo.java new file mode 100644 index 0000000..45963cd --- /dev/null +++ b/src/main/java/Iterable/IterableDemo.java @@ -0,0 +1,39 @@ +package Iterable; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +public class IterableDemo { + public static void run() { + System.out.println("=== Iterable Demo ==="); + + List names = new ArrayList<>(); + + names.add("Kenneth"); + + names.add("Kenny"); + + names.add("Ken"); + + System.out.println("Enhanced for-loop:"); + + for (String name : names) { + System.out.println(name); + + } + + System.out.println(); + + System.out.println("Iterator:"); + + Iterator iterator = names.iterator(); + + while (iterator.hasNext()) { + + System.out.println(iterator.next()); + } + + System.out.println(); + } +} \ No newline at end of file diff --git a/src/main/java/Lists/ListsDemo.java b/src/main/java/Lists/ListsDemo.java new file mode 100644 index 0000000..b6a8be5 --- /dev/null +++ b/src/main/java/Lists/ListsDemo.java @@ -0,0 +1,60 @@ +package Lists; + +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; +import java.util.Stack; +import java.util.Vector; + +public class ListsDemo { + public static void run() { + System.out.println("=== Lists Demo ==="); + + List arrayList = new ArrayList<>(); + + arrayList.add("A"); + + arrayList.add("B"); + + arrayList.add("C"); + + System.out.println("ArrayList: " + arrayList); + + System.out.println("ArrayList get(1): " + arrayList.get(1)); + + List linkedList = new LinkedList<>(); + + linkedList.add("X"); + + linkedList.add("Y"); + + linkedList.add("Z"); + + System.out.println("LinkedList: " + linkedList); + + Vector vector = new Vector<>(); + + vector.add(10); + + vector.add(20); + + vector.add(30); + System.out.println("Vector: " + vector); + + Stack stack = new Stack<>(); + + stack.push("Bottom"); + + stack.push("Middle"); + + stack.push("Top"); + + System.out.println("Stack before pop: " + stack); + + System.out.println("Popped: " + stack.pop()); + + System.out.println("Stack after pop: " + stack); + + System.out.println(); + } +} \ No newline at end of file diff --git a/src/main/java/Main.java b/src/main/java/Main.java new file mode 100644 index 0000000..ddb3c9c --- /dev/null +++ b/src/main/java/Main.java @@ -0,0 +1,28 @@ +import Collections.CollectionsDemo; +import CollectionsHackerrank.CollectionsHackerrankDemo; +import Hierarchy.HierarchyDemo; +import Iterable.IterableDemo; +import Lists.ListsDemo; +import Maps.MapsDemo; +import Queues.QueuesDemo; +import Sets.SetsDemo; + +public class Main { + public static void main(String[] args) { + HierarchyDemo.run(); + + IterableDemo.run(); + + CollectionsDemo.run(); + + ListsDemo.run(); + + SetsDemo.run(); + + QueuesDemo.run(); + + MapsDemo.run(); + + CollectionsHackerrankDemo.run(); + } +} \ No newline at end of file diff --git a/src/main/java/Maps/MapsDemo.java b/src/main/java/Maps/MapsDemo.java new file mode 100644 index 0000000..a363798 --- /dev/null +++ b/src/main/java/Maps/MapsDemo.java @@ -0,0 +1,52 @@ +package Maps; + +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.TreeMap; + +public class MapsDemo { + public static void run() { + + System.out.println("=== Maps Demo ==="); + + Map hashMap = new HashMap<>(); + + hashMap.put(1, "One"); + + hashMap.put(2, "Two"); + + hashMap.put(3, "Three"); + + System.out.println("HashMap: " + hashMap); + + Map linkedHashMap = new LinkedHashMap<>(); + linkedHashMap.put("Apples", 5); + + linkedHashMap.put("Bananas", 3); + + linkedHashMap.put("Oranges", 8); + + System.out.println("LinkedHashMap: " + linkedHashMap); + + Map treeMap = new TreeMap<>(); + treeMap.put(30, "C"); + + treeMap.put(10, "A"); + + treeMap.put(20, "B"); + System.out.println("TreeMap: " + treeMap); + + System.out.println("hashMap.get(2): " + hashMap.get(2)); + System.out.println("hashMap.containsKey(3): " + hashMap.containsKey(3)); + + System.out.println("Loop through HashMap:"); + + for (Map.Entry entry : hashMap.entrySet()) { + + System.out.println("Key: " + entry.getKey() + ", Value: " + entry.getValue()); + } + + System.out.println(); + } +} \ No newline at end of file diff --git a/src/main/java/Queues/QueuesDemo.java b/src/main/java/Queues/QueuesDemo.java new file mode 100644 index 0000000..f4efc92 --- /dev/null +++ b/src/main/java/Queues/QueuesDemo.java @@ -0,0 +1,71 @@ +package Queues; + +import java.util.ArrayDeque; +import java.util.Deque; +import java.util.LinkedList; +import java.util.PriorityQueue; +import java.util.Queue; + +public class QueuesDemo { + public static void run() { + + System.out.println("=== Queues Demo ==="); + + Queue queue = new LinkedList<>(); + + queue.offer("First"); + + queue.offer("Second"); + + queue.offer("Third"); + + System.out.println("Queue: " + queue); + + System.out.println("Queue poll(): " + queue.poll()); + + System.out.println("Queue after poll: " + queue); + + Deque deque = new LinkedList<>(); + + deque.offerFirst("Front"); + + deque.offerLast("Back"); + + deque.offerFirst("New Front"); + + System.out.println("Deque: " + deque); + + System.out.println("Deque pollFirst(): " + deque.pollFirst()); + + System.out.println("Deque pollLast(): " + deque.pollLast()); + + System.out.println("Deque after removals: " + deque); + + ArrayDeque arrayDeque = new ArrayDeque<>(); + + arrayDeque.offer(100); + + arrayDeque.offer(200); + + arrayDeque.offer(300); + + System.out.println("ArrayDeque: " + arrayDeque); + + PriorityQueue priorityQueue = new PriorityQueue<>(); + + priorityQueue.offer(40); + + priorityQueue.offer(10); + + priorityQueue.offer(30); + + priorityQueue.offer(20); + System.out.println("PriorityQueue: " + priorityQueue); + + System.out.println("PriorityQueue poll(): " + priorityQueue.poll()); + + System.out.println("PriorityQueue after poll: " + priorityQueue); + + System.out.println(); + } +} \ No newline at end of file diff --git a/src/main/java/Sets/SetsDemo.java b/src/main/java/Sets/SetsDemo.java new file mode 100644 index 0000000..d382fdd --- /dev/null +++ b/src/main/java/Sets/SetsDemo.java @@ -0,0 +1,49 @@ +package Sets; + +import java.util.HashSet; +import java.util.LinkedHashSet; +import java.util.Set; +import java.util.TreeSet; + +public class SetsDemo { + public static void run() { + + System.out.println("=== Sets Demo ==="); + + Set hashSet = new HashSet<>(); + + hashSet.add("Dog"); + + hashSet.add("Cat"); + + hashSet.add("Dog"); + + System.out.println("HashSet: " + hashSet); + + Set linkedHashSet = new LinkedHashSet<>(); + + linkedHashSet.add("Red"); + + linkedHashSet.add("Blue"); + + linkedHashSet.add("Green"); + + linkedHashSet.add("Blue"); + + System.out.println("LinkedHashSet: " + linkedHashSet); + + Set treeSet = new TreeSet<>(); + + treeSet.add(50); + + treeSet.add(10); + + treeSet.add(30); + + treeSet.add(10); + + System.out.println("TreeSet: " + treeSet); + + System.out.println(); + } +} \ No newline at end of file