Collection vs Collections in Java with Example

In this article, we will learn about Collection vs Collections in Java with Example.

Collection

Collection is an interface in java.util.package. That is used to represent a group of individual objects as a unit. It is similar to the container in the C ++ language. The collection is considered the root interface of the collection framework. It offers several classes and interfaces to represent a group of individual objects as a unit.

List, Set, and Queue are the most important subinterfaces of the Collect interface. The map interface is also part of the Java collection framework but does not inherit the interface collection. The most important methods of the collection interface are add(), remove(), clear(), size(), and contains().

Syntax

public interface Collection<E> extends Iterable<E>
Type Parameters: E - the type of elements returned by this iterator

Collections

Collections is a utility class that exists in java.util.package. It defines several utility methods, such as sort and finds, which are used to work on the collection. It has all the static methods. These methods are convenient for developers and also work effectively with the Collection Framework. For example, it has a sort () method to sort the collection items according to the standard sort order, and it has a min () and max () method to find the minimum and maximum values ​​in the collection items. , respectively.

Syntax

public class Collections extends Object
CollectionCollections
It is an interface.It is a utility class.
Used to represent a group of individual objects as a unit.Defines several utility methods that are used to work with the collection.
An interface that contains a static method of java8. The interface can also contain abstract and standard methods.Only contains static methods.

Example

import java.io.*;
import java.util.*;

class HandyOpinion{
	
	public static void main (String[] args)
	{
		
	// Creating an object of List<String>
	List<String> arrlist = new ArrayList<String>();
		
	// Adding elements to arrlist
	arrlist.add("Handy");
	arrlist.add("Opinion");
	arrlist.add("website");
		
	// Printing the elements of arrlist
	// before operations
	System.out.println("Elements of arrlist before the operations = ");
	System.out.println(arrlist);
		
	System.out.println("Elements of arrlist after the operations = ");
		
	// Adding all the specified elements
	// to the specified collection
	Collections.addAll(arrlist, "New", "Article");
		
	// Printing the arrlist after
	// performing addAll() method
	System.out.println(arrlist);
		
	// Sorting all the elements of the
	// specified collection according to
	// default sorting order
	Collections.sort(arrlist);
		
	// Printing the arrlist after
	// performing sort() method
	System.out.println(arrlist);
		
	}
}

Output

Elements of arrlist before the operations:
[Handy, Opinion, website]
Elements of arrlist after the operations:
[Handy, Opinion, website, New, Article]
[website, Handy, Opinion, Article, New]

That’s all we will learn about Collection vs Collections in Java with Example.

Next Article

1. PriorityQueue in Java
2. Stack Class in Java
3. Queue Interface In Java
4. List Interface in Java with Examples

Please share this post:
Posts created 70

Ask a Question

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

%d bloggers like this: