Both Collection and Collections in Java, have some similarities like
- Both (Collection and Collections) are part of the Java Collections Framework.
- Both (Collection and Collections) are present in java.util package.
- Both (Collection and Collections) are added to jdk in java version 1.2
Besides these similarities, they have few differences also.
Collection
Collection interface represents the root interface in the collection hierarchy. It contains the core methods for all collections. List, Set and Queue are immediate sub interfaces of Collection interface.
Map interface is also part of Java collection framework but it does not inherit the Collection interface.
Syntax:
public interface Collectionextends Iterable
Collection Interface Methods
add() | It is used to add the new element in the specified collection. |
remove(Object o) | It is used to remove the existing element from the specified collection. |
size() | It is used to get the number of elements in the specified collection. |
clear() | It is used to remove the all elements from the specified collection. |
isEmpty() | It is used to check whether the specified collection contains any element or not. It returns true if one or more element present the collection otherwise returns false. |
Collections
Collections class in java represents an utility class in java.util package. It contains exclusively static methods that operate on or return collections. It consists of polymorphic algorithms that operate on collections, “wrappers”, which in turn return a new collection backed by a specified collection.
Syntax:
public class Collections extends Object
Collections Class Methods
Collections.sort() | It is used to sort the elements in the specified collection. |
Collections.min() | It is used to get the minimum element in the specified collection. |
Collections.max() | It is used to get the maximum element in the specified collection. |
Collections.binarySearch() | It is used to search the specified element in the given collection. It uses binary search algorithm internally. |
Collections.reverse() | It is used to reverse the order of elements in the specified collection. |
Collections.copy() | It is used to copy all elements from one collection to the another collection. |
Collections.shuffle() | It is used to randomly shuffles the elements in the specified collection. |
Collections.synchronizedCollection() | It is used to synchronize the specified collection. |
Collections.disjoint() | It returns true if two given collections have no elements in common, otherwise returns false. |
Example
import java.util.*; public class Main { public static void main(String args[]) { //Createing ArrayList Object ArrayList<String> students = new ArrayList<>(); //Adding elements to the ArrayList students.add("Jai"); students.add("Manish"); students.add("Vikram"); students.add("Mahesh"); students.add("Naren"); //Print ArrayList System.out.println("ArrayList elements: " + students); // Print minimum value System.out.println("Minimum value: " + Collections.min(students)); // print maximum value System.out.println("Maximum value: " + Collections.max(students)); } } |
Output
ArrayList elements: [Jai, Manish, Vikram, Mahesh, Naren] Minimum value: Jai Maximum value: Vikram |
Difference between Collection and Collections
Collection | Collections |
It represents root level interface of Java Collection framework. | It represents as utility class of Java Collection framework which consists of only static methods that operate on or return collections. |
Since Java 8, It can contains static, abstract and default methods. | It can only contains static methods. |
It extends iterable interface. | It extends Object class. |
Java interview questions on collections
- What is the difference between arraylist and vector in java?
- What is the difference between arraylist and linkedlist?
- What is the difference between Iterator and ListIterator?
- What is the difference between Iterator and Enumeration?
- what is the difference between list and set in java?
- what is the difference between set and map in java?
- what is the difference between hashset and treeset in java?
- what is the difference between hashset and hashmap in java?
- what is the difference between hashmap and treemap in java?
- what is the difference between hashmap and hashtable in java?
- what is the difference between collection and collections in java?
- what is the difference between comparable and comparator interfaces?
- what is the hashcode method in java?
- Java equals method
- Java hashCode method
- Why to override hashcode and equals method in java?
- How hashmap works intrnally?
- How put and get works in hashmap?
- How to resolve collision in hashmap?
- How hashmap stores null key?
- How hashset works intrnally?