Collection Framework Simplified in Java

0
29
Spread the love
  • 1
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    1
    Share

Collection is an important concept in java. The collection framework is a part of java.util package.The collection framework defines a set of interfaces and their implementation to manipulate collections. They serve as a container for a group of Objects such as a set of words in a dictionary or a collection of emails etc.The collection framework also allows us to store,retrieve and update a set of objects.
Advantages of collection:

  • It is grow able in nature, based on programmer or application need , this can be increased or decreased.
  • It can hold homogeneous or heterogeneous data
  • Every collection is backed by some data structure [ready made data structure]

To overcome problem of array it is important to use collection.

Collection:  Collection is way of representation of a group of objects as a single entity.In general collection interface is considered as root interface of collection framework.
Collection Framework: It is group of several classes and interfaces which can be used to represent a group of objects a single entity.

So basically a collection is a set of elements. Methods defined in a collection Interface as given as:


add(Object obj);// returns true if the object is added to the specified collection.
adAll(Collection c);//returns true if the objects of the collection c are added to the specified collection.
remove(Object obj);//returns true if the object is removed from the specified collection.
removeAll(Collection c)//returns true if the objects of the collection c are removed from the specified collection.
clear();//removes all elements from the specified collection
contains(Object obj);////returns true if the collection contains the specified object.
containsAll(Collection c);//Returns true if the collection contains all the elements in the specified collection.
equals(Object obj);//Returns true if the specified object matches with the object in the collection.
hashCode();//returns the hashcode of the collection.
isEmpty();//Returns true if the collection is empty.
iterator();Returns iterator over the elements in the collection.
retainAll(Collection c);//Returns true if all the elements in a collection c is retrained in the specified 
//collection.
size();//returns the number of elements in the collection.
toArray();//returns an array containing all of the elements in the collection.
toArray(Object[] object) ;//returns an array of objects if the array contains all the elements in the 
//specified collection.
 

add(),addAll() and remove() modify the collection objects.They may throw an Exception-UnsupportedOperationException when the collection does not support respective operation.
The methods of the collection interface will throw the ClassCastException if we add an incompatible element to a collection.(Some collection may not support null elements.)

Collection is an interface which can be used to represent a group of objects as  a single entity.
Collections is an utility class found in java.util package to define several utility methods for collection objects.
The different frameworks are written below:

Class Interface
AbstractCollection Collection
AbstractList List
AbstractQueue Queue
PriorityQueue Queue
AbstractSequentialList List
LinkedList List
AbstractSet Set
EnumSet Set
HashSet Set
TreeSet Set
ArrayList List,Cloneable,Serializable
Vector List,Cloneable,Serializable
Stack List,Cloneable,Serializable
Hashtable List,Cloneable,Serializable

The classes available in the collection framework implements the collection interface and the sub Interfaces. These classes also implement Map and Iterator Interfaces.
I am  writing on these one by one.

Don't miss out!
Subscribe To Newsletter

Receive top technical news, lesson ideas, travel tips and more!

Invalid email address
Give it a try. You can unsubscribe at any time.

Spread the love
  • 1
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    1
    Share

LEAVE A REPLY

Please enter your comment!
Please enter your name here