|
| 1 | +package com.notes.collection; |
| 2 | + |
| 3 | +public class CollectionNotes { |
| 4 | + /* |
| 5 | + * Java Collections Framework: |
| 6 | + * |
| 7 | + * Imagine you have a bunch of things you want to keep organized |
| 8 | + * maybe a list of your favorite books, |
| 9 | + * a set of unique toys, or a line of people waiting for a ride. |
| 10 | + * In Java, the Collections Framework |
| 11 | + * helps you do exactly that with groups of objects. |
| 12 | + * It's like a toolkit filled with different ways |
| 13 | + * to store and manage your data. |
| 14 | + * |
| 15 | + * What is it? |
| 16 | + * ----------- |
| 17 | + * The Collections Framework is a set of tools |
| 18 | + * (interfaces and classes) in Java that makes working |
| 19 | + * with groups of objects much easier. It's found in the java.util package. |
| 20 | + * |
| 21 | + * Key Ideas: |
| 22 | + * ---------- |
| 23 | + * - Collection: |
| 24 | + * This is the most basic idea |
| 25 | + * it simply means a group of objects. |
| 26 | + * |
| 27 | + * - Interfaces: |
| 28 | + * Think of these as blueprints or contracts. |
| 29 | + * They define what actions can be |
| 30 | + * performed on a collection (like adding or removing items), |
| 31 | + * but they don't provide the actual implementation. |
| 32 | + * |
| 33 | + * - Classes: |
| 34 | + * These are the actual implementations of the interfaces. |
| 35 | + * They provide the specific code that makes the actions work. |
| 36 | + * |
| 37 | + * - Generics: |
| 38 | + * This is a way to make sure you're only putting the correct type |
| 39 | + * of objects into your collection. |
| 40 | + * For example, a "List of Strings" can only hold String objects. |
| 41 | + * This helps prevent errors. |
| 42 | + * |
| 43 | + * - Data Structures: |
| 44 | + * These are different ways of organizing data, |
| 45 | + * each with its own advantages |
| 46 | + * and disadvantages in terms of speed and memory usage. |
| 47 | + * |
| 48 | + * |
| 49 | + * The Main Types of Collections: |
| 50 | + * ----------------------------- |
| 51 | + * 1. List (Ordered and Allows Duplicates): |
| 52 | + * |
| 53 | + * - A List is like a numbered list. |
| 54 | + * Each item has a specific position (index), and you can |
| 55 | + * have duplicate items. |
| 56 | + * |
| 57 | + * - Examples: |
| 58 | + * |
| 59 | + * - ArrayList: |
| 60 | + * Like a dynamic array that can grow as needed. |
| 61 | + * Great for accessing items |
| 62 | + * quickly by their position, |
| 63 | + * but adding or removing items in the middle can be slower. |
| 64 | + * |
| 65 | + * |
| 66 | + * - LinkedList: |
| 67 | + * Like a chain of linked boxes. |
| 68 | + * Adding or removing items is quick, |
| 69 | + * but finding |
| 70 | + * a specific item by its position can be slower. |
| 71 | + * |
| 72 | + * |
| 73 | + * - Vector: |
| 74 | + * An older, thread-safe version of ArrayList. |
| 75 | + * Thread-safe means it's designed |
| 76 | + * to be used by multiple parts of a program at the same time |
| 77 | + * without causing problems. |
| 78 | + * |
| 79 | + * |
| 80 | + * - Stack: |
| 81 | + * A special type of list where you add and remove |
| 82 | + * items from the top (like a stack of plates). |
| 83 | + * This is called LIFO (Last-In, First-Out). |
| 84 | + * |
| 85 | + * |
| 86 | + * - Useful List Actions: |
| 87 | + * - add(index, item): Put an item at a specific position. |
| 88 | + * - get(index): Get the item at a specific position. |
| 89 | + * - set(index, item): Replace the item at a specific position. |
| 90 | + * - remove(index): Remove the item at a specific position. |
| 91 | + * |
| 92 | + * |
| 93 | + * 2. Set (Unordered and Does Not Allow Duplicates): |
| 94 | + * |
| 95 | + * - A Set is like a bag of unique items. |
| 96 | + * You can't have duplicates. The order of items is |
| 97 | + * not guaranteed. |
| 98 | + * - Examples: |
| 99 | + * - HashSet: |
| 100 | + * Uses a technique called "hashing" to store items quickly. |
| 101 | + * Very fast for checking if an item is in the set. |
| 102 | + * - TreeSet: |
| 103 | + * Stores items in a sorted order. |
| 104 | + * |
| 105 | + * |
| 106 | + * 3. Queue (Ordered and Follows FIFO): |
| 107 | + * - A Queue is like a line of people. |
| 108 | + * The first person in line is the first one to be served |
| 109 | + * (FIFO: First-In, First-Out). |
| 110 | + * - Examples: |
| 111 | + * - LinkedList: Can also be used as a Queue. |
| 112 | + * - PriorityQueue: Items are retrieved based on their priority |
| 113 | + * (e.g., the most important task gets done first). |
| 114 | + * |
| 115 | + * |
| 116 | + * Moving Through Collections (Iteration): |
| 117 | + * -------------------------------------- |
| 118 | + * - Iterator: An object that lets you go through a collection one item at a time. |
| 119 | + * - ListIterator: A special type of Iterator for Lists that allows you to go both forward |
| 120 | + * and backward. |
| 121 | + * - for-each loop: A simpler way to go through all the items in a collection. |
| 122 | + * - forEach() method: A more modern way (introduced in Java 8) to do something with each item |
| 123 | + * in a collection using a concise syntax. |
| 124 | + * |
| 125 | + * Important Notes: |
| 126 | + * --------------- |
| 127 | + * - Thread Safety: Most collections are not designed to be used safely by multiple parts of |
| 128 | + * a program at the same time (not thread-safe). If you need this, you'll need special |
| 129 | + * "concurrent" collections or ways to synchronize access. |
| 130 | + * - Fixed-Size and Immutable Collections: |
| 131 | + * - Arrays.asList(): Creates a list that cannot grow or shrink. |
| 132 | + * - List.of(): Creates a list that cannot be changed at all (immutable). |
| 133 | + * - Performance: Different collections have different performance characteristics. ArrayList is |
| 134 | + * fast for getting items by their position, but LinkedList is faster for adding/removing items. |
| 135 | + * - The Collections class: This is a utility class with helpful methods for working with |
| 136 | + * collections, such as sorting and searching. |
| 137 | + */ |
| 138 | +} |
0 commit comments