Difference Between Collection and Collections



 This post summaries the difference between Collection and Collections.

Difference Between Collection and Collections

Here is the main difference between Collection and Collections:

  • Collection is an interface that can be used to represent a group of individual objects as a Single Entity.
  • Whereas Collections is a utility class present in java.util package to define several utility methods for Collection objects.

Collection Interface Example

Below example demonstrates how to create a Collection and add new elements to it:
import java.util.ArrayList;
import java.util.Collection;

public class CreateCollectionInterfaceExample {

    public static void main(String[] args) {
        // Creating an ArrayList of String using 
     Collection<String> animals = new ArrayList<>();
        // Adding new elements to the ArrayList
        animals.add("Lion");
        animals.add("Tiger");
        animals.add("Cat");
        animals.add("Dog");
        System.out.println(animals);
        
        String[] arrayOfAnimals = new String[4];
        System.out.println(arrayOfAnimals.length);
        arrayOfAnimals[0] = "Lion";
        arrayOfAnimals[1] = "Tiger";
        arrayOfAnimals[2] = "Cat";
        arrayOfAnimals[3] = "Dog";
        for (String string : arrayOfAnimals) {
            System.out.println(string);
        }        
    }
}

Collections Utility Class Example


The Collections class provides static methods whose first argument is the collection on which the operation is to be performed The great majority of the algorithms provided by the Java platform operate on List instances, but a few of them operate on arbitrary Collection instances. The Collections class provides static methods for below Algorithms:
  • Sorting
  • Shuffling
  • Routine Data Manipulation
  • Searching
  • Composition
  • Finding Extreme Values

Sorting using Collections Class

List<String> list = new LinkedList<>();
list.add("element 2");
list.add("element 1");
list.add("element 4");
list.add("element 3");
// Sorts the specified list into ascending order, according to
// the natural ordering of its elements.
Collections.sort(list);
for (String str : list) {
 System.out.println(" sort elements in ascending order  --" + str);
}
Output:
 sort elements in ascending order  --element 1
 sort elements in ascending order  --element 2
 sort elements in ascending order  --element 3
 sort elements in ascending order  --element 4

Shuffling using java.util.Collections Class

private static void shuffleAlgorithmsDemo() {
    List<String> list = new LinkedList<>();
    list.add("element 2");
    list.add("element 1");
    list.add("element 4");
    list.add("element 3");

    Collections.sort(list);
    for (String str : list) {
        System.out.println(" sort elements in ascending order  --" + str);
    }

    // randomly permutes the elements in a List.
    Collections.shuffle(list);
    for (String str : list) {
        System.out.println(" sort elements in ascending order  --" + str);
    }
}
Output:
 sort elements in ascending order  --element 2
 sort elements in ascending order  --element 3
 sort elements in ascending order  --element 4
 shuffle elements  --element 1
 shuffle elements  --element 3
 shuffle elements  --element 4
 shuffle elements  --element 2

Comments