The Collection interface (java.util.Collection) is one of the root interfaces of the Java collection classes. It declares the core methods that all collections will have. Because all collections implement Collection interface, so we might familiar with its methods for a clear understanding of the framework.
The following shows the
The following shows the
Collection
interface:public interface Collection<E> extends Iterable<E> {
// Basic operations
int size();
boolean isEmpty();
boolean contains(Object element);
// optional
boolean add(E element);
// optional
boolean remove(Object element);
Iterator<E> iterator();
// Bulk operations
boolean containsAll(Collection<?> c);
// optional
boolean addAll(Collection<? extends E> c);
// optional
boolean removeAll(Collection<?> c);
// optional
boolean retainAll(Collection<?> c);
// optional
void clear();
// Array operations
Object[] toArray();
<T> T[] toArray(T[] a);
}
Following table specifies the public methods of collection interface:
Example:
package com.sample.javase.testing;
import java.util.ArrayList;
import java.util.Collection;
public class CollectionInterfaceExample {
/**
* @param args
*/
public static void main(String[] args) {
Collection<String> collection = new ArrayList<String>();
collection.add("javase");
collection.add("javaee");
collection.add("javame");
//We can print the collection as follows or we can iterate the collection.
System.out.println(collection);
}
}
No comments:
Post a Comment