The Collection interface provides a set of common methods that are available to most collection types in the Java Collection Framework. These methods allow developers to perform basic operations such as adding, removing, searching, and managing elements in a collection.
Key Points: • Common methods are defined in the Collection interface. • They provide a consistent way to work with different collection types. • These methods help manage elements efficiently. • Most implementations such as ArrayList, LinkedList, HashSet, and PriorityQueue support them. • They simplify collection manipulation without depending on specific implementations.
Common Collection Methods:
1. add(E element)
Adds an element to the collection.
Example:
collection.add("Java");
2. remove(Object obj)
Removes the specified element from the collection.
Example:
collection.remove("Java");
3. size()
Returns the total number of elements in the collection.
Example:
int count = collection.size();
4. isEmpty()
Checks whether the collection contains any elements.
Example:
collection.isEmpty();
5. clear()
Removes all elements from the collection.
Example:
collection.clear();
6. contains(Object obj)
Checks whether a specific element exists in the collection.
Example:
collection.contains("Java");
7. iterator()
Returns an Iterator for traversing collection elements.
Example:
Iterator<String> iterator =
collection.iterator();8. addAll(Collection c)
Adds all elements from another collection.
Example:
collection1.addAll(collection2);
9. removeAll(Collection c)
Removes all matching elements found in another collection.
Example:
collection1.removeAll(collection2);
10. retainAll(Collection c)
Keeps only the elements that are present in both collections.
Example:
collection1.retainAll(collection2);
11. containsAll(Collection c)
Checks whether all elements of another collection exist in the current collection.
Example:
collection1.containsAll(collection2);
12. toArray()
Converts the collection into an array.
Example:
Object[] array =
collection.toArray();Example: The following example demonstrates some commonly used collection methods.
Code Example:
import java.util.ArrayList;
import java.util.Collection;
public class Demo {
public static void main(String[] args) {
Collection<String> skills =
new ArrayList<>();
skills.add("Java");
skills.add("Spring");
System.out.println(
"Size: " + skills.size());
System.out.println(
"Contains Java: "
+ skills.contains("Java"));
skills.remove("Spring");
System.out.println(skills);
System.out.println(
"Is Empty: "
+ skills.isEmpty());
}
}Output:
Size: 2 Contains Java: true [Java] Is Empty: false
Why These Methods Are Useful:
• Provide a common API across collections • Reduce implementation-specific coding • Simplify data management • Improve code readability • Promote reusable programming practices
Interview Tip: A concise interview answer is:
"Some common methods available in the Collection interface are add(), remove(), size(), isEmpty(), clear(), contains(), iterator(), addAll(), removeAll(), retainAll(), containsAll(), and toArray(). These methods provide a consistent way to add, remove, search, and manage elements across different collection types."