Collections

  •  The collections concept was introduced in the java 1.2 version to resolve the problems of arrays.
  • Collections can hold a group of objects.
  • The collection size is not fixed which means as we are inserting and deleting the elements the size of the collection dynamically increased or decreased.
  • Every collection internally follows data structures and contains predefined functions it means we not need to write new logic for performing the operations like Insertion, Deletion, and Searching. Sorting etc.

Java

Array:

An array is a collection of elements that are stored in continuous memory locations.

Limitations of array (or) disadvantages of the array:

  1. The size of an array is fixed which means once the array size is decided it can't be increased or decreased. With these times memory may be wasted or sometimes memory may not be sufficient.
  2. There are no pre-defined functions to perform the operations like Insertion, Deletion, Searching, Sorting etc. so as a programmer we have to write our own logic.
  3.  arrays store only a group of similar elements Because of the problems in an array the Java people have come up with a collection framework.

Collection object

  • An object is said to be a collection object if it holds or stores a group of other objects.
  • Collection never stores any primitive values, but if we want to store primitive values we have to represent primitive values as objects.

Collection classes

  • A collection class is a class whose object can store a group of other objects.

  • Example: ArrayList,  HashSet, HashMap.

collection stores a group of objects like follows.


collection stored Structure

  • Here objects o1,o2, o3 and o4 are stored 2 times there by we are wasting the memory within the JVM.
  • To save the memory within the JVM when the objects are stored in the collection object, the JVM stores the references of the objects within the collection objects instead of storing the copy of objects directly.

collection JVM Structure


  • All the collection classes are available in the "java.util" (utility) package.
  • All the collection interfaces and collection classes together as a collection framework.

  • All the collection classes are classified into three categories.

List:

  • This category is used to store a group of individual elements where the elements can be duplicated.
  • List is an Interface whose object can not be created directly.
  • To work with this category we have to use the following implementations class of list interface ArrayList, Linked list, Vector, Stack

Set

  • This category is used to store a group of individual elements. But the elements can't be duplicated.
  • Set is an interface whose object can not be created directly.
  • To work with this category we have to use the following implementations class of Set interface HashSet, LinkedHashSet and TreeSet.

Collections Structure

  •  Both List and Set are extended from the Collection interface.


Map:

  • This category is used to store the element in the form of key-value pairs where the keys can't be duplicated, values can be duplicated
  • Map is an interface whose object can not be created directly.
  • To work with this category we have to use the following implementation classes of Map interface HashMap, LinkedHashMap, TreeMap,  Hashtable.




  •  The map is part of the Collection Framework but not extending from the Collection interface.



What is ArrayList




Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.