Collection Types and Classes – a summary Collection Interface Definition Method Summary Addition/removal boolean add(Object o) Ensures that this collection contains the specified element (optional operation). boolean addAll(Collection c) Adds all of the elements in the specified collection to this collection (optional operation). void clear() Removes all of the elements from this collection (optional operation). boolean remove(Object o) Removes a single instance of the specified element from this collection, if it is present (optional operation). boolean removeAll(Collection c) Removes all this collection's elements that are also contained in the specified collection (optional operation). boolean retainAll(Collection c) Retains only the elements in this collection that are contained in the specified collection (optional operation). Containment boolean contains(Object o) Returns true if this collection contains the specified element. boolean containsAll(Collection c) Returns true if this collection contains all of the elements in the specified collection. Navigation Iterator iterator() Returns an iterator over the elements in this collection. Inherited from the Object class boolean equals(Object o) Compares the specified object with this collection for equality. int hashCode() Returns the hash code value for this collection. Sizing boolean isEmpty() Returns true if this collection contains no elements. int size() Returns the number of elements in this collection. Convenient Object[] toArray() Returns an array containing all of the elements in this collection. Object[] toArray(Object[] a) Returns an array containing all of the elements in this collection; the runtime type of the returned array is that of the specified array. The root interface in the collection hierarchy. A collection represents a group of objects, known as its elements. Some collections allow duplicate elements and others do not. Some are ordered and others unordered. The SDK does not provide any direct implementations of this interface: it provides implementations of more specific subinterfaces like Set and List. This interface is typically used to pass collections around and manipulate them where maximum generality is desired. Set Interface Definition Method Summary Addition/removal boolean add(Object o) Adds the specified element to this set if it is not already present (optional operation). boolean addAll(Collection c) Adds all of the elements in the specified collection to this set if they're not already present (optional operation). void clear() Removes all of the elements from this set (optional operation). boolean remove(Object o) Removes the specified element from this set if it is present (optional operation). boolean removeAll(Collection c) Removes from this set all of its elements that are contained in the specified collection (optional operation). boolean retainAll(Collection c) Retains only the elements in this set that are contained in the specified collection (optional operation). Containment boolean contains(Object o) Returns true if this set contains the specified element. boolean containsAll(Collection c) Returns true if this set contains all of the elements of the specified collection. Navigation Iterator iterator() Returns an iterator over the elements in this set. Inherited from the Object class boolean equals(Object o) Compares the specified object with this set for equality. int hashCode() Returns the hash code value for this set. Sizing boolean isEmpty() Returns true if this set contains no elements. int size() Returns the number of elements in this set (its cardinality). Convenient Object[] toArray() Returns an array containing all of the elements in this set. Object[] toArray(Object[] a) Returns an array containing all of the elements in this set; the runtime type of the returned array is that of the specified array. A collection that contains no duplicate elements. More formally, sets contain no pair of elements e1 and e2 such that e1.equals(e2), and at most one null element. As implied by its name, this interface models the mathematical set abstraction. The Set interface places additional stipulations, beyond those inherited from the Collection interface, on the contracts of all constructors and on the contracts of the add, equals and hashCode methods. Declarations for other inherited methods are also included here for convenience. (The specifications accompanying these declarations have been tailored to the Set interface, but they do not contain any additional stipulations.) The additional stipulation on constructors is, not surprisingly, that all constructors must create a set that contains no duplicate elements (as defined above). SortedSet Interface Definition Method Summary Comparator comparator() Returns the comparator associated with this sorted set, or null if it uses its elements' natural ordering. Object first() Returns the first (lowest) element currently in this sorted set. Object last() Returns the last (highest) element currently in this sorted set. SortedSet headSet(Object toElement) Returns a view of the portion of this sorted set whose elements are strictly less than toElement. SortedSet subSet(Object fromElement, Object toElement) Returns a view of the portion of this sorted set whose elements range from fromElement, inclusive, to toElement, exclusive. SortedSet tailSet(Object fromElement) Returns a view of the portion of this sorted set whose elements are greater than or equal to fromElement. A set that further guarantees that its iterator will traverse the set in ascending element order, sorted according to the natural ordering of its elements (see Comparable), or by a Comparator provided at sorted set creation time. Several additional operations are provided to take advantage of the ordering. (This interface is the set analogue of SortedMap.) All elements inserted into an sorted set must implement the Comparable interface (or be accepted by the specified Comparator). Furthermore, all such elements must be mutually comparable: e1.compareTo(e2) (or comparator.compare(e1, e2)) must not throw a ClassCastException for any elements e1 and e2 in the sorted set. Attempts to violate this restriction will cause the offending method or constructor invocation to throw a ClassCastException. List Interface Definition Method Summary Addition/removal void add(int index, Object element) Inserts the specified element at the specified position in this list (optional operation). boolean add(Object o) Appends the specified element to the end of this list (optional operation). boolean addAll(Collection c) Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's iterator (optional operation). boolean addAll(int index, Collection c) Inserts all of the elements in the specified collection into this list at the specified position (optional operation). void clear() Removes all of the elements from this list (optional operation). Object remove(int index) Removes the element at the specified position in this list (optional operation). boolean remove(Object o) Removes the first occurrence in this list of the specified element (optional operation). boolean removeAll(Collection c) Removes from this list all the elements that are contained in the specified collection (optional operation). boolean retainAll(Collection c) Retains only the elements in this list that are contained in the specified collection (optional operation). Containment boolean contains(Object o) Returns true if this list contains the specified element. boolean containsAll(Collection c) Returns true if this list contains all of the elements of the specified collection. Setters/getters Object get(int index) Returns the element at the specified position in this list. int indexOf(Object o) Returns the index in this list of the first occurrence of the specified element, or -1 if this list does not contain this element. int lastIndexOf(Object o) Returns the index in this list of the last occurrence of the specified element, or -1 if this list does not contain this element. Object set(int index, Object element) Replaces the element at the specified position in this list with the specified element (optional operation). List subList(int fromIndex, int toIndex) Returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive. Navigation Iterator iterator() Returns an iterator over the elements in this list in proper sequence. ListIterator listIterator() Returns a list iterator of the elements in this list (in proper sequence). ListIterator listIterator(int index) Returns a list iterator of the elements in this list (in proper sequence), starting at the specified position in this list. Inherited from the Object class boolean equals(Object o) Compares the specified object with this list for equality. int hashCode() Returns the hash code value for this list. Sizing boolean isEmpty() Returns true if this list contains no elements. int size() Returns the number of elements in this list. Convenient Object[] toArray() Returns an array containing all of the elements in this list in proper sequence. Object[] toArray(Object[] a) Returns an array containing all of the elements in this list in proper sequence; the runtime type of the returned array is that of the specified array. An ordered collection (also known as a sequence). The user of this interface has precise control over where in the list each element is inserted. The user can access elements by their integer index (position in the list), and search for elements in the list. Unlike sets, lists typically allow duplicate elements. More formally, lists typically allow pairs of elements e1 and e2 such that e1.equals(e2), and they typically allow multiple null elements if they allow null elements at all. It is not inconceivable that someone might wish to implement a list that prohibits duplicates, by throwing runtime exceptions when the user attempts to insert them, but we expect this usage to be rare. Map Interface Definition Method Summary Addition/removal void clear() Removes all mappings from this map (optional operation). Object put(Object key, Object value) Associates the specified value with the specified key in this map (optional operation). void putAll(Map t) Copies all of the mappings from the specified map to this map (optional operation). Object remove(Object key) Removes the mapping for this key from this map if it is present (optional operation). Containment boolean containsKey(Object key) Returns true if this map contains a mapping for the specified key. boolean containsValue(Object value) Returns true if this map maps one or more keys to the specified value. Getters/setters Object get(Object key) Returns the value to which this map maps the specified key. Set entrySet() Returns a set view of the mappings contained in this map. Set keySet() Returns a set view of the keys contained in this map. Collection values() Returns a collection view of the values contained in this map. Inherited from the Object class boolean equals(Object o) Compares the specified object with this map for equality. int hashCode() Returns the hash code value for this map. Sizing boolean isEmpty() Returns true if this map contains no key-value mappings. int size() Returns the number of key-value mappings in this map. An object that maps keys to values. A map cannot contain duplicate keys; each key can map to at most one value. This interface takes the place of the Dictionary class, which was a totally abstract class rather than an interface. The Map interface provides three collection views, which allow a map's contents to be viewed as a set of keys, collection of values, or set of key-value mappings. The order of a map is defined as the order in which the iterators on the map's collection views return their elements. Some map implementations, like the TreeMap class, make specific guarantees as to their order; others, like the HashMap class, do not. Note: great care must be exercised if mutable objects are used as map keys. The behavior of a map is not specified if the value of an object is changed in a manner that affects equals comparisons while the object is a key in the map. A special case of this prohibition is that it is not permissible for a map to contain itself as a key. While it is permissible for a map to contain itself as a value, extreme caution is advised: the equals and hashCode methods are no longer well defined on a such a map. Map.Entry (Enclosed) Interface Definition Method Summary boolean equals(Object o) Compares the specified object with this entry for equality. Object getKey() Returns the key corresponding to this entry. Object getValue() Returns the value corresponding to this entry. int hashCode() Returns the hash code value for this map entry. Object setValue(Object value) Replaces the value corresponding to this entry with the specified value (optional operation). public static interface Map.Entry A map entry (key-value pair). The Map.entrySet method returns a collection-view of the map, whose elements are of this class. The only way to obtain a reference to a map entry is from the iterator of this collection-view. These Map.Entry objects are valid only for the duration of the iteration; more formally, the behavior of a map entry is undefined if the backing map has been modified after the entry was returned by the iterator, except through the iterator's own remove operation, or through the setValue operation on a map entry returned by the iterator. entrySet public Set entrySet() Returns a set view of the mappings contained in this map. Each element in the returned set is a Map.Entry. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. If the map is modified while an iteration over the set is in progress, the results of the iteration are undefined. The set supports element removal, which removes the corresponding mapping from the map, via the Iterator.remove, Set.remove, removeAll, retainAll and clear operations. It does not support the add or addAll operations. Returns: a set view of the mappings contained in this map. SortedMap Interface Definition Method Summary Comparator comparator() Returns the comparator associated with this sorted map, or null if it uses its keys' natural ordering. Object firstKey() Returns the first (lowest) key currently in this sorted map. SortedMap headMap(Object toKey) Returns a view of the portion of this sorted map whose keys are strictly less than toKey. Object lastKey() Returns the last (highest) key currently in this sorted map. SortedMap subMap(Object fromKey, Object toKey) Returns a view of the portion of this sorted map whose keys range from fromKey, inclusive, to toKey, exclusive. SortedMap tailMap(Object fromKey) Returns a view of the portion of this sorted map whose keys are greater than or equal to fromKey. Collection Implementations Classes that implement the collection interfaces typically have names of the form <Implementation-style><Interface>. The general purpose implementations are summarized in the table below: Implementations Hash Table Resizable Array Balanced Tree Set HashSet ArrayList Interfaces List Map HashMap Linked List TreeSet Hash Table + Linked List LinkedHashSet LinkedList TreeMap LinkedHashMap public class TreeMap extends AbstractMap implements SortedMap, Cloneable, Serializable Red-Black tree based implementation of the SortedMap interface. This class guarantees that the map will be in ascending key order, sorted according to the natural order for the key's class (see Comparable), or by the comparator provided at creation time, depending on which constructor is used. public class LinkedHashMap extends HashMap Hash table and linked list implementation of the Map interface, with predictable iteration order. This implementation differs from HashMap in that it maintains a doubly-linked list running through all of its entries. This linked list defines the iteration ordering, which is normally the order in which keys were inserted into the map (insertion-order). public class HashMap extends AbstractMap implements Map, Cloneable, Serializable Hash table based implementation of the Map interface. This implementation provides all of the optional map operations, and permits null values and the null key. (The HashMap class is roughly equivalent to Hashtable, except that it is unsynchronized and permits nulls.) This class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time. Enumerator Interface Definition Method Summary boolean hasMoreElements() Tests if this enumeration contains more elements. Object nextElement() Returns the next element of this enumeration if this enumeration object has at least one more element to provide. Comparator Interface Definition Method Summary int compare(Object o1, Object o2) Compares its two arguments for order. boolean equals(Object obj) Indicates whether some other object is "equal to" this Comparator. Iterator Interface Definition Method Summary boolean hasNext() Returns true if the iteration has more elements. Object next() Returns the next element in the iteration. void remove() Removes from the underlying collection the last element returned by the iterator (optional operation). ListIterator Interface Definition Method Summary void add(Object o) Inserts the specified element into the list (optional operation). boolean hasNext() Returns true if this list iterator has more elements when traversing the list in the forward direction. boolean hasPrevious() Returns true if this list iterator has more elements when traversing the list in the reverse direction. Object next() Returns the next element in the list. int nextIndex() Returns the index of the element that would be returned by a subsequent call to next. Object previous() Returns the previous element in the list. int previousIndex() Returns the index of the element that would be returned by a subsequent call to previous. void remove() Removes from the list the last element that was returned by next or previous (optional operation). void set(Object o) Replaces the last element returned by next or previous with the specified element (optional operation).
© Copyright 2026 Paperzz