In Java, the Collections Framework provides a set of interfaces and classes that make it easier to work with groups of objects. The framework includes several interfaces such as List, Set, Queue, and Map, each with their own set of methods and operations. In this article, we will explore the common methods and operations provided by these interfaces.
The List interface extends the Collection interface and allows for the storage of ordered elements. Some of the common methods provided by the List interface include:
boolean add(E element)
: Appends the specified element to the end of the list.void add(int index, E element)
: Inserts the specified element at the specified position in the list.boolean remove(Object element)
: Removes the first occurrence of the specified element from the list.E get(int index)
: Returns the element at the specified position in the list.int size()
: Returns the number of elements in the list.boolean isEmpty()
: Returns true if the list contains no elements.void clear()
: Removes all elements from the list.The Set interface extends the Collection interface and represents a collection of unique elements. Some of the common methods provided by the Set interface include:
boolean add(E element)
: Adds the specified element to the set if it is not already present.boolean remove(Object element)
: Removes the specified element from the set.boolean contains(Object element)
: Returns true if the set contains the specified element.int size()
: Returns the number of elements in the set.boolean isEmpty()
: Returns true if the set contains no elements.void clear()
: Removes all elements from the set.Iterator<E> iterator()
: Returns an iterator over the elements in the set.The Queue interface extends the Collection interface and represents a collection of elements with specific insertion and removal behavior. Some of the common methods provided by the Queue interface include:
boolean add(E element)
: Inserts the specified element into the queue.boolean offer(E element)
: Adds the specified element to the queue.E remove()
: Retrieves and removes the head of the queue.E poll()
: Retrieves and removes the head of the queue, or returns null if the queue is empty.E element()
: Retrieves, but does not remove, the head of the queue.E peek()
: Retrieves, but does not remove, the head of the queue, or returns null if the queue is empty.int size()
: Returns the number of elements in the queue.The Map interface represents a mapping between a key and a value. It does not extend the Collection interface, but it is an essential part of the Java Collections Framework. Some of the common methods provided by the Map interface include:
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, or null if the map contains no mapping for the key.boolean containsKey(Object key)
: Returns true if the map contains a mapping for the specified key.boolean containsValue(Object value)
: Returns true if the map maps one or more keys to the specified value.V remove(Object key)
: Removes the mapping for the specified key from the map.int size()
: Returns the number of key-value mappings in the map.boolean isEmpty()
: Returns true if the map contains no key-value mappings.These are just some of the commonly used methods and operations provided by the Java Collections Interfaces. By utilizing these methods, developers can easily manipulate and organize groups of objects efficiently and effectively. Understanding these common methods is essential for anyone working with Java Collections.
noob to master © copyleft