Java - Difference Between Collection and Collections

By Shahnail Khan Last updated : March 22, 2024

In Java programming, when we talk about managing groups of objects, we often encounter two terms: Collection and Collections. While they might sound similar, they serve different purposes.

Suppose you have a bunch of toys scattered around your room. You want to organize them neatly, so it's easy to find what you need when you want to play. This organization is like what Collection does in Java – it helps manage and organize groups of objects.

Now, think of Collections as the tools you use to organize those toys. You might use a basket to gather them up or sort them into different boxes based on their types. Collections in Java are like those tools – they provide methods to sort, search, and manipulate groups of objects efficiently.

In this article, we'll see the differences between Collection and Collections, which will help you understand when to use each and how they play different roles in Java programming.

What is Collection in Java?

In Java, a Collection is an interface that represents a group of objects known as elements. It provides a unified architecture to work with different types of collections, such as lists, sets, and queues, allowing for manipulation, traversal, and storage of elements.

Example of Collection in Java

Suppose we have a list of favourite fruits that we want to store and manage in a Java program.

import java.util.ArrayList;

public class Main {
  public static void main(String[] args) {
    // Creating an ArrayList
    ArrayList < String > favoriteFruits = new ArrayList < > ();

    // Adding fruits to the collection
    favoriteFruits.add("Apple");
    favoriteFruits.add("Banana");
    favoriteFruits.add("Orange");

    // Printing all favorite fruits
    System.out.println("My favorite fruits:");
    for (String fruit: favoriteFruits) {
      System.out.println(fruit);
    }
  }
}

The output of the above example is:

My favorite fruits:
Apple
Banana
Orange

In this example, we create a List collection named Main. We add three fruits (Apple, Banana, and Orange) to this collection using the add() method. Finally, we iterate over the collection using a for loop and display each fruit.

Explanation:

  • Here, List<String> represents a collection that holds strings (in this case, the names of fruits).
  • ArrayList<> is a specific implementation of the List interface that provides dynamic array functionality.
  • favoriteFruits.add("Apple") adds the fruit "Apple" to the collection.
  • The loop iterates over each fruit in the collection and displays it.

What is Collections in Java?

In Java, Collections refers to a framework that provides a set of classes and interfaces to work with groups of objects, commonly known as collections. These collections can hold multiple elements, like lists, sets, maps, and queues. This makes it easier for developers to manage, manipulate, and process data efficiently within their programs. The Collections framework offers various operations, such as adding, removing, searching, and sorting elements.

Example of Collections in Java

Suppose we have a list of car brand names that we want to store and perform sorting.

import java.util.ArrayList;
import java.util.Collections;

public class Cars {
  public static void main(String[] args) {
    // Creating a List collection to store cars
    ArrayList < String > carBrand = new ArrayList < > ();

    // Adding car brands to the collection
    carBrand.add("Volkswagen");
    carBrand.add("Toyota Motors");
    carBrand.add("Daimler");

    // Sorting 
    Collections.sort(carBrand);

    // Displaying the car brands in sorted manner
    System.out.println("Popular Car brands are: ");
    for (String car: carBrand) {
      System.out.println(car);
    }
  }
}

The output of the above example is:

Popular Car brands are: 
Daimler
Toyota Motors
Volkswagen

Differences Between Collection and Collections in Java

Aspect Collection Collections
Nature Interface Utility Class
Purpose Represents a collection of objects. Provides utility methods for collections.
Usability Used to define collections and their operations. Used to perform operations on collections.
Instantiation Cannot be instantiated directly. Does not require instantiation.
Methods Defines methods like add(), remove(), size(), etc. Provides static methods like sort(), shuffle().
Inheritance Part of java.util package. Part of java.util package.
Mutability Depends on the implementation. Immutable.
Use Case Manipulating collections directly. Performing operations on collections.
Extensibility Can be extended to create custom collections. Cannot be extended.
Examples List, Set, Queue, etc. sort(), shuffle(), binarySearch(), etc.
Implementation Implemented by various classes like ArrayList, LinkedList, etc. Implemented as a final class with a private constructor.

Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.