- Is use to store the Object this is also known as element.
- Collection is a dynamic in size.
- Collection can store the different types of values.
- There are different criteria’s to store objects inside collection.
- Collection can be Order.
- Collection can be sorted.
- Collection can be of unique elements
- Collection can have duplicate elements.
- Collection majorly divides into 3 types
- List: To Store duplicate elements, and to store element based on index.
- Set: To store unique elements.
- Queue: is to set elements using FIFO order.
- All the classes, interfaces are present inside util.
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.
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.
- By Using Iterator
- By Using for loop
- By Using ListIterator
Collections Utility class
- Collections is a utility class which has static function.
- These functionalities are use to perform operation on existing collection.
Generic Type to a Collection
- You can specify the type of values to be store inside collection by specifying a generic type.
- If you do not specify generic type then Java will give a warning.
- And this may cause a Casting exception during run time.
- 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
- You can only use class name as a generic type, and primitive data types are not allowed as a generic type.
List<Integer> list = new ArrayList<Integer>();
Wrapper Class In Java
- Is the class which is provided for each primitive data type.
- These wrapper classes are use to store value and perform operation on the values.
- Boxing and Un-Boxing
- 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)
- 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)
- Boxing and un-boxing happen internally by java which is known as auto-boxing.
Join Telegram : Click Here