97
Java Collection Interface
Collection is a group of objects, which are known as elements. It is the root interface in the collection hierarchy. This interface is basically used to pass around the collections and manipulate them where the maximum generality is desired.
There are many methods defined in the Collection interface. These are as follows:
Method | Description |
---|---|
add() | This method returns a Boolean value true if it inserts the specified element in this collection. |
addAll() | This method returns a Boolean value true if it adds all the elements of specified collection in the invoking collection. |
clear() | It removes all the elements automatically from this collection. |
contains() | It returns a Boolean value true if this queue contains the specified element. |
containsAll() | It returns a Boolean value true if this collection contains all the elements in the specified collection. |
equals() | This method returns a boolean value true if the specified object is equal with this collection. |
hashCode() | It returns a hash code value for this collection. |
isEmpty() | This method returns true if this collection contains no elements or is empty. |
iterator() | It returns an iterator over the elements in proper sequence. |
remove() | It removes the specified element from this queue, if it is present in the collection. |
removeAll() | It removes all the elements of this collection which are also present in the specified collection. |
removeIf() | It removes all the elements of this collection that satisfy the given predicate filter. |
retainAll() | This method retains only those elements in this collection that are present in the specified collection. |
size() | It returns the total number of the elements in this collection. |
spliterator() | It returns a spliterator over the elements in this collection. |
toArray() | It returns an array containing all the elements of this collection which are in proper sequence. |
Example 1
Output:
Initial collection :[2, 5] Final Collection : [2, 5, 11, 12, 13, 14, 15] Size of Collection : 7 5 is present in the collection Elements in collection : []
Example 2
Output:
Collection : [11, 12, 13, 14, 15] 11 12 13 14 15 Queue is empty Elements in the set : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] Multiple of 5 : [5, 10, 15, 20]
Next TopicJava Collection