A collection refers to a set of items, referred to as elements, and serves as the foundational interface within the collection hierarchy. It is primarily utilized for transferring collections and performing operations on them when a high level of generality is needed.
Numerous techniques are specified within the Collection interface. These include:
| 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
Example
Example
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
public class Main {
public static void main(String[] args) {
Set<Integer> set = new HashSet<>();
// inserts the specified element in this collection
set.add(2);
set.add(5);
System.out.println("Initial collection :"+set);
//it adds all the elements of specified collection in the invoking collection.
Collections.addAll(set, 11, 12, 13, 14, 15);
System.out.println("Final Collection : "+set);
//returns the total size of the collection
int size =set.size();
System.out.println("Size of Collection : "+size);
//It returns a Boolean value true if this queue contains the specified element.
Boolean val=set.contains(5);
if (val){
System.out.println("5 is present in the collection");
}
else{
System.out.println("5 is not present in the collection");
}
//It removes all the elements automatically from this collection.
set.clear();
System.out.println("Elements in collection : "+set);
}
}
Output:
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
Example
Example
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.ConcurrentLinkedQueue;
public class Main {
public static void main(String[] args) {
ConcurrentLinkedQueue<Integer> queue = new ConcurrentLinkedQueue<Integer>();
Set<Integer> set = new HashSet<>();
//it adds all the elements of specified collection in the invoking collection.
Collections.addAll(set, 11, 12, 13, 14, 15);
System.out.println("Collection : "+set);
//It returns an iterator over the elements in proper sequence.
Iterator<Integer> iterator = set.iterator();
while(iterator.hasNext()){
System.out.println(iterator.next());
}
set.clear();
//checks whether the set is empty or not
Boolean b1= set.isEmpty();
if (b1){
System.out.println("Queue is empty");
}
else{
System.out.println("Queue is not empty");
}
for (int i=1;i<21;i++){
queue.add(i);
}
System.out.println(" Elements in the set : " + queue);
for (int i = 1; i < 11; i++) {
int j = i *5;
set.add(j);
}
//will give the elements in the queue which are present in set
queue.retainAll(set);
System.out.println(" Multiple of 5 : " + queue);
}
}
Output:
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]