Sunday, November 6, 2016

Difference between Collection and Collections


collection
collections
Collection & collections
Interface
Root interface for java collection Framework(all the collection interface (List,Set,Queue) are sub interface of it.
Class
Class is a member of Java collection framework.
Interfaces and its implementations classes together form collection framework.
Map interface, doesn’t inherit from Collection interface
Member of java.util package.
Contains abstract methods.

public interface Collection<E>
extends Iterable<E>

Utility class of java.util package.
Contains static method to operate on object of type collection.
public class Collections extends Object
Both are from java.util package.
ArrayList is a class that , implements List interface(sub interface of Collection Interfae)
e.g.
ArrayList<Integer> arrList = new ArrayList<Integer>()
Collections class provide static utility methdos to operate on the object of ArryaList.
e.g. Collections.sort(arrList)

Collection ß  Set,List,Queue

Set ß SortedSetß NavigableSet
Queueß Deque
public class Collections extends Object
Contains methods to perform basic operations.

int size()
boolean isEmpty()               
boolean contains(Object o)                
Iterator<E> iterator()            
Object[] toArray()  
<T> T[] toArray(T[] a)           
boolean add(E e)
boolean remove(Object o)
boolean containsAll(Collection<?> c)
boolean addAll(Collection<? extends E> c)
boolean removeAll(Collection<?> c)             
boolean retainAll(Collection<?> c)                
void clear()            
boolean equals(Object o)
int hashCode()
Stream<E> stream() -> java 8 and later
Stream<E> parallelStream()->java 8 and later

iterator is the only one inherited form Iterator interface.

Some methods from Collections class, to perform operation on collection object.

Collections.max()               
Collections.min()
Collections.sort()
Collections.shuffle()
Collections.synchronizedCollection()               
Collections.binarySearch()               
Collections.disjoint()          
Collections.copy()              
Collections.reverse()

No comments:

Post a Comment