Collection In Java

Collection

  1. Is use to store the Object this is also known as element.
  2. Collection is a dynamic in size.
  3. Collection can store the different types of values.
  4. There are different criteria’s to store objects inside collection.
    1. Collection can be Order.
    2. Collection can be sorted.
    3. Collection can be of unique elements
    4. Collection can have duplicate elements.
  5. Collection majorly divides into 3 types
    1. List: To Store duplicate elements, and to store element based on index.
    2. Set: To store unique elements.
    3. Queue: is to set elements using FIFO order.
  6. All the classes, interfaces are present inside util.

 

Collection Methods

 

The Collection in Java is a framework that provides an architecture to store and manipulate the group of objects.

Java Collection framework provides many interfaces (Set, List, Queue, Deque) and classes (ArrayList, Vector, LinkedList, PriorityQueue, HashSet, LinkedHashSet, TreeSet).

 

What is a Framework?

A framework is a set of classes and interfaces which provide a ready-made architecture. In order to implement a new feature or a class, there is no need to define a framework. However, an optimal object-oriented design always includes a framework with a collection of classes such that all the classes perform the same kind of task.

 

Collection Hierarchy

The utility package, (java.util) contains all the classes and interfaces that are required by the collection framework. The collection framework contains an interface named an iterable interface which provides the iterator to iterate through all the collections. This interface is extended by the main collection interface which acts as a root for the collection framework. All the collections extend this collection interface thereby extending the properties of the iterator and the methods of this interface. The following figure illustrates the hierarchy of the collection framework.

Collection

 

Iterate Collection.

  1. By Using Iterator
  2. By Using for loop
  3. By Using ListIterator

 

 

Collections Utility class

  1. Collections is a utility class which has static function.
  2. These functionalities are use to perform operation on existing collection.

 

 

Generic Type to a Collection

  1. You can specify the type of values to be store inside collection by specifying a generic type.
  2. If you do not specify generic type then Java will give a warning.
  3. And this may cause a Casting exception during run time.
  4. To avoid casting and casting exception can specify generic type, by which you restrict user to store only a specific type of elements inside collection
  5. You can only use class name as a generic type, and primitive data types are not allowed as a generic type.
  6. Example:

List<Integer> list = new ArrayList<Integer>();

 

Wrapper Class In Java

  1. Is the class which is provided for each primitive data type.
  2. These wrapper classes are use to store value and perform operation on the values.
PrimitiveWrapper class
intInteger
byteByte
shortShort
longLong
floatFloat
doubleDouble
charCharacter
booleanBoolean
  1. Boxing and Un-Boxing
    1. Boxing – Is a process in which you can convert primitive value into wrapper class.

int a =10;

Integer x = a; // Boxing (primitive to Wrapper conversion)

 

  1. Un-Boxing – Is a process in which you can convert wrapper class value into primitive.

Integer y = new Integer(20);

int b = y; // un-boxing (Wrapper to Primitive conversion)

 

  1. Boxing and un-boxing happen internally by java which is known as auto-boxing.

 

 

 

Join Telegram : Click Here

 

All Full Stack Java Study Material

 

Java the programing language

 

Job’s For Fresher

 

Share This Information To Your Friends and Your College Group’s, To Help Them !