Collection vs Collections

 Collections



Collections is an Utility Class.It is found in java.util.package.

Syntax:

public class Collections extends Object

It contains several utility methods that helps us to operate on the collection Interface.
It is the root interface, that contains several sub interfaces like Set, List, DeQueue and Queue.
Here most of the methods are static.
They are sort(), min(), max(), reverse(), copy() are some of the methods that we use on the collection.

eg)
List<Integer> list = new ArrayList<>();
list.add(12);
list.add(32);
list.add(34);
list.add(1);
list.add(9);

Collections.sort(list);                 // here collections class method is performed on the collection
System.out.print(list);
Collections.reverse(list);
System.out.print(list);

Output:
[1,9,12,32,34]
[34,32,12,9,1]

Collection

Collection is an Interface found in the java.util.package.

Syntax

public interface Collection<E> extends Iterable<E>
Here E specifies the type of element returned by the Iterator

It is collection of objects of Similar type.
From Java 8, collection Interface Contains static Methods. 
It also contains default and abstract methods.

Some of the important methods of collection Interface are add(), remove(), clear(), size() etc.

eg)
List<Integer> list = new ArrayList<>();
list.add(12);
list.add(32);
list.add(34);
list.add(1);
list.add(9);
System.out.print(list);

Output:
[12,32,34,1,9]

Comments

Popular posts from this blog

Why Pointers are Eliminated in Java?

What is the advancement made in Hashmap in Java 8?

Integer.parseInt(“text”)