Wrapped Collections and Collections Class

Wrapped Collections and Collections Class In Java, the Collections class is a utility class that provides various static methods to operate on or return collections. The Wrapped Collections refers to collections that are decorated with additional behavior like thread safety or immutability. Wrapped Collections Wrapped collections are used to extend the functionality of existing collections. The Collections class in Java provides methods like synchronizedList(), unmodifiableList(), and others to “wrap” a collection with added functionality. ...

May 8, 2025 · 3 min · Rohan

Map and Sortedmap

Map and SortedMap In Java, the Map and SortedMap interfaces are part of the Java Collections Framework. The Map interface represents a collection of key-value pairs, where each key is unique, and each key maps to exactly one value. The SortedMap interface extends Map and guarantees that the keys are sorted in a specific order. Map Interface The Map interface defines a collection of key-value pairs. The key is used to retrieve its corresponding value. Common implementations of Map include HashMap, LinkedHashMap, and TreeMap. ...

May 8, 2025 · 3 min · Rohan

List

List The List interface is a part of the Java Collections Framework and represents an ordered collection that can contain duplicate elements. Unlike Set, a List maintains the order in which elements are inserted and allows for random access to its elements via an index. Key Implementations of List ArrayList: An ArrayList is a resizable array implementation of the List interface. It provides fast random access to elements but can be slower when adding or removing elements at arbitrary positions (except at the end). List<String> names = new ArrayList<>(); names.add("Alice"); names.add("Bob"); 2. **LinkedList**: * A `LinkedList` is a doubly linked list implementation of the `List` interface. It offers better performance than `ArrayList` when adding or removing elements from the beginning or middle of the list. ```java List<String> names = new LinkedList<>(); names.add("Alice"); names.add("Bob"); Vector: ...

May 8, 2025 · 2 min · Rohan

Set and Sortedset

Set and SortedSet In Java, the Set and SortedSet interfaces represent collections of unique elements. While the Set interface simply enforces uniqueness, the SortedSet interface builds upon Set by maintaining its elements in a sorted order. Both interfaces are part of the Java Collections Framework and are primarily used when uniqueness and ordering are required. Set Interface The Set interface extends the Collection interface and does not allow duplicate elements. The most commonly used implementations of Set are: ...

May 8, 2025 · 2 min · Rohan

Collection Interface

Collection Interface The Collection Interface is the root interface of the Java Collections Framework. It represents a group of objects, known as elements. The Collection interface provides fundamental methods to work with elements, such as adding, removing, and querying elements in a collection. All other collection interfaces (Set, List, Queue, etc.) extend this interface. Key Methods in Collection Interface add(E e): Adds the specified element to the collection. Returns true if the element was successfully added. collection.add("Alice"); remove(Object o): ...

May 8, 2025 · 2 min · Rohan

Iteration

Iteration Iteration in Java refers to the process of iterating or traversing through a collection (such as a List, Set, or Map) to access its elements. Iteration is a crucial aspect of working with collections in Java and can be performed using various techniques like for, while, do-while loops, and using the Iterator interface. Iterating Over Collections Using Different Methods Using For-each Loop: The simplest and most concise method of iterating over collections. List<String> names = Arrays.asList("Alice", "Bob", "Charlie"); for (String name : names) { System.out.println(name); } 2. **Using Iterator**: * The `Iterator` interface provides methods like `hasNext()` and `next()` to iterate through the elements in a collection. ```java Iterator<String> iterator = names.iterator(); while (iterator.hasNext()) { System.out.println(iterator.next()); } Using forEach Method (Java 8+): ...

May 8, 2025 · 2 min · Rohan

Collections

Collections In Java, the Collections Framework is a unified architecture for representing and manipulating collections of objects. It provides a set of interfaces, implementations, and algorithms to handle various types of data collections like lists, sets, and maps. The java.util package contains all the collection classes and interfaces. Core Interfaces of Collections Framework Collection Interface: The root interface of the collection hierarchy. It defines common operations like add(), remove(), size(), and clear(), which are implemented by all other collection classes. Set Interface: ...

May 8, 2025 · 2 min · Rohan