java.util
package.
public interface Collection<E> extends Iterable<E>
{
// methods (abstract and default)
}
Collection
Interface:
Collection
interface:
Sr. No. | Method | Description |
---|---|---|
1 | boolean add(E e) |
Adds a single element to the collection. Returns true if the collection changed. |
2 | boolean addAll(Collection<? extends E> c) |
Adds all elements from another collection. Returns true if the collection changed. |
3 | boolean contains(Object o) |
Checks if the collection contains the specified element. Returns true if found. |
4 | boolean isEmpty() |
Checks if the collection has no elements. Returns true if empty. |
5 | boolean remove(Object o) |
Removes a single instance of the specified element. Returns true if removed. |
6 | void clear() |
Removes all elements from the collection, making it empty. |
7 | int size() |
Returns the number of elements in the collection. |
8 | Iterator<E> iterator() |
Returns an Iterator to traverse elements one by one. |
9 | Object[] toArray() |
Converts the collection into an array of Object . |
10 | <T> T[] toArray(T[] a) |
Converts the collection into an array of the specified type. |
11 | boolean containsAll(Collection<?> c) |
Checks if the collection contains all elements of the specified collection. |
12 | boolean removeAll(Collection<?> c) |
Removes all elements in this collection that are also contained in the specified collection. |
13 | boolean retainAll(Collection<?> c) |
Retains only the elements in this collection that are also contained in the specified collection. |
14 | default boolean removeIf(Predicate<? super E> filter) |
Removes all elements that satisfy the given predicate. (Java 8+) |
15 | default Stream<E> stream() |
Returns a sequential Stream with this collection as the source. (Java 8+) |
16 | default Stream<E> parallelStream() |
Returns a possibly parallel Stream with this collection as the source. (Java 8+) |
17 | default Spliterator<E> spliterator() |
Creates a Spliterator over the elements in this collection. (Java 8+) |
ArrayList
, HashSet
, or LinkedList
.
List
, Set
, Queue
).
import java.util.*;
public class CollectionDemo
{
public static void printCollection(Collection<String> collection)
{
for (String element : collection)
{
System.out.println(element);
}
}
public static void main(String[] args)
{
Collection<String> list = new ArrayList<>();
list.add("Amit");
list.add("Deepak");
printCollection(list);
System.out.println("-----------");
Collection<String> set = new HashSet<>();
set.add("Kamal");
set.add("Rahul");
printCollection(set);
}
}
Amit Deepak ----------- Rahul Kamal
Collection
reference when we want our code should work with any type of collection (List, Set, or Queue) and we don't need special features like indexed access.
add()
, remove()
, or iterate
, using Collection
makes it easy to switch between different collection types later.
Collection
in their APIs so that developers can pass any kind of collection without changing their code.
Collection<E>
(E represents the type of elements).ArrayList
, HashSet
, LinkedList
) implement it, directly or indirectly.
Your feedback helps us grow! If there's anything we can fix or improve, please let us know.
Weβre here to make our tutorials better based on your thoughts and suggestions.