Iterable Interface
The Iterable
interface is the root interface for all collection classes. It represents a collection that can be traversed, typically using a “for-each” loop.
Iterator<T> iterator()
: Returns an iterator over elements of type T
.
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class IterableExample {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("Apple");
list.add("Banana");
list.add("Orange");
// Using the iterator() method
Iterator<String> iterator = list.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
}
}
Collection Interface
The Collection
interface is a group of objects, known as its elements. It is the root of the collection hierarchy.
boolean add(E e)
: Adds an element to the collection.boolean remove(Object o)
: Removes a single instance of the specified element.boolean contains(Object o)
: Returnstrue
if the collection contains the specified element.int size()
: Returns the number of elements in the collection.void clear()
: Removes all elements from the collection.boolean isEmpty()
: Returnstrue
if the collection contains no elements.
import java.util.ArrayList;
import java.util.Collection;
public class CollectionExample {
public static void main(String[] args) {
Collection<String> collection = new ArrayList<>();
collection.add("Apple");
collection.add("Banana");
collection.add("Orange");
System.out.println("Collection size: " + collection.size());
System.out.println("Contains Banana? " + collection.contains("Banana"));
collection.remove("Banana");
System.out.println("Collection size after removal: " + collection.size());
}
}
Set Interface
The Set
interface extends the Collection
interface and represents a collection that does not allow duplicate elements.
It has all methods from the Collection.
import java.util.HashSet;
import java.util.Set;
public class SetExample {
public static void main(String[] args) {
Set<String> set = new HashSet<>();
set.add("Apple");
set.add("Banana");
set.add("Apple"); // Duplicate, will not be added
System.out.println("Set size: " + set.size());
for (String fruit : set) {
System.out.println(fruit);
}
}
}
List Interface
The List
interface extends the Collection
interface and represents an ordered collection. Lists can contain duplicate elements.
E get(int index)
: Returns the element at the specified position.E set(int index, E element)
: Replaces the element at the specified position.void add(int index, E element)
: Inserts the element at the specified position.E remove(int index)
: Removes the element at the specified position.int indexOf(Object o)
: Returns the index of the first occurrence of the specified element.int lastIndexOf(Object o)
: Returns the index of the last occurrence of the specified element.
import java.util.ArrayList;
import java.util.List;
public class ListExample {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("Apple");
list.add("Banana");
list.add("Orange");
System.out.println("Element at index 1: " + list.get(1));
list.add(1, "Grapes");
System.out.println("Element at index 1 after addition: " + list.get(1));
list.remove(2);
System.out.println("List after removal: " + list);
}
}
Queue Interface
The Queue
interface extends the Collection
interface and represents a collection designed for holding elements prior to processing. Typically, queues order elements in a FIFO (first-in-first-out) manner.
boolean offer(E e)
: Inserts the specified element into the queue.E poll()
: Retrieves and removes the head of the queue, or returnsnull
if the queue is empty.E peek()
: Retrieves, but does not remove, the head of the queue, or returnsnull
if the queue is empty.
import java.util.LinkedList;
import java.util.Queue;
public class QueueExample {
public static void main(String[] args) {
Queue<String> queue = new LinkedList<>();
queue.offer("Apple");
queue.offer("Banana");
queue.offer("Orange");
System.out.println("Head of the queue: " + queue.peek());
queue.poll();
System.out.println("Head of the queue after poll: " + queue.peek());
}
}
Deque Interface
The Deque
interface extends the Queue
interface and represents a double-ended queue that supports element insertion and removal at both ends.
void addFirst(E e)
: Inserts the specified element at the front of the deque.void addLast(E e)
: Inserts the specified element at the end of the deque.E removeFirst()
: Retrieves and removes the first element of the deque.E removeLast()
: Retrieves and removes the last element of the deque.E getFirst()
: Retrieves, but does not remove, the first element of the deque.E getLast()
: Retrieves, but does not remove, the last element of the deque.
import java.util.ArrayDeque;
import java.util.Deque;
public class DequeExample {
public static void main(String[] args) {
Deque<String> deque = new ArrayDeque<>();
deque.addFirst("Apple");
deque.addLast("Banana");
deque.addFirst("Orange");
System.out.println("First element: " + deque.getFirst());
System.out.println("Last element: " + deque.getLast());
deque.removeFirst();
System.out.println("First element after removal: " + deque.getFirst());
}
}
Map Interface
The Map
interface represents a mapping between a key and a value. It does not extend the Collection
interface.
V put(K key, V value)
: Associates the specified value with the specified key in the map.V get(Object key)
: Returns the value to which the specified key is mapped, ornull
if the map contains no mapping for the key.V remove(Object key)
: Removes the mapping for a key from the map.boolean containsKey(Object key)
: Returnstrue
if the map contains a mapping for the specified key.boolean containsValue(Object value)
: Returnstrue
if the map maps one or more keys to the specified value.Set<K> keySet()
: Returns a set view of the keys contained in the map.Collection<V> values()
: Returns a collection view of the values contained in the map.
import java.util.HashMap;
import java.util.Map;
public class MapExample {
public static void main(String[] args) {
Map<String, Integer> map = new HashMap<>();
map.put("Apple", 1);
map.put("Banana", 2);
map.put("Orange", 3);
System.out.println("Value for key 'Banana': " + map.get("Banana"));
map.remove("Banana");
System.out.println("Map after removing 'Banana': " + map);
System.out.println("Keys: " + map.keySet());
System.out.println("Values: " + map.values());
}
}
SortedMap Interface
The SortedMap
interface extends the Map
interface and represents a map that maintains its keys in a sorted order.
Comparator<? super K> comparator()
: Returns the comparator used to order the keys in this map, ornull
if the map uses the natural ordering of its keys.K firstKey()
: Returns the first (lowest) key currently in the map.K lastKey()
: Returns the last (highest) key currently in the map.SortedMap<K, V> subMap(K fromKey, K toKey)
: Returns a view of the portion of this map whose keys range fromfromKey
, inclusive, totoKey
, exclusive.SortedMap<K, V> headMap(K toKey)
: Returns a view of the portion of this map whose keys are strictly less thantoKey
.SortedMap<K, V> tailMap(K fromKey)
: Returns a view of the portion of this map whose keys are greater than or equal tofromKey
.
import java.util.SortedMap;
import java.util.TreeMap;
public class SortedMapExample {
public static void main(String[] args) {
SortedMap<String, Integer> sortedMap = new TreeMap<>();
sortedMap.put("Apple", 1);
sortedMap.put("Banana", 2);
sortedMap.put("Orange", 3);
System.out.println("First key: " + sortedMap.firstKey());
System.out.println("Last key: " + sortedMap.lastKey());
SortedMap<String, Integer> headMap = sortedMap.headMap("Orange");
System.out.println("Head map: " + headMap);
}
}