Collection Framework

Hola Amigos.

 This is my first blog post. I hope i can help you understand java easily. So lets get started with Java Collection Topic.

Note:- I took help from oracle docs. I've simplified it for you. If you need any more information goto oracle docs .

Collection :-    A collection is an object that groups multiple elements into a single unit. 

Use of Collection :- Collection is used for storing, manipulating and communicate aggregate data.

Collection Framework :- It is a unified architecture for representing and manipulating collections. 


Collection Framework contains :-
  • Interface (Collection, Set, List, etc.)
  • Implementation (Classes)
  • Algorithm
Advantage of Collection  :- Using collection API in our program will reduce programming efforts, increase program speed and quality. Java has very rich set of API which makes programmer's task easy. Unlike C, C++ you don't have to write the whole complex implementation, All you need to do in java is to create an object of appropriate class. 

For instance, In C, if you want to create a link list, You have to create a structure and the you have to write the lengthy and complex code.
but in java, all you need to write is one line,
LinkedList l= new LinkedList();
Here, This l is an linked list class object. With this one line, you have a linked list. 


Interfaces :-




1-Dimensional Collection  :- In 1-D Collection framework, data is organised in the form of either row or column.

Collection interface :-

Collection interface is the root of collection hierarchy. It is used to pass collection and manipulate them when maximum generality is required.
Collection interface is generic (Feature added in java SE 5.0).  For example,

public interface Collection extends Iterable<E>{

                                                                // some code

                                                      }

<E>  tells you that the interface is generic. 

When declaring a Collection instance you should specify the type of object you are going to add into the Collection. Specifying the type allows the compiler to verify at compile time that the type of object you are putting into the Collection is correct. Therefore, reducing errors at run time.

For example,
                     Collection<String> c=new ArrayList<>();
c.add("Ankita");
c.add("Sumit");
                                                          c.add(29);                                       //will be an error

Third line will be an compile time error (incompatible type) because we have specified that the ArrayList will contain elements of type String but we are entering an integer.

Set Interface :- 

Set is a sub-interface of Collection.Set interface is similar to Collection with a difference that Set does not allow duplicate elements. Set implementations are HashSet, TreeSet, LinkedHashSet.

List Interface :- 

List also known as sequence. List is an ordered collection ( The elements will remain in the same order you put them in). It provides you precise control over where in the list each element is inserted and can access them by index. List implementations are ArrayList, LinkedList.

Queue Interface :-

Queue is a collection which is used to hold multiple elements prior to processing ( because of it's FIFO nature). Queue typically, but not necessarily, order elements in a FIFO manner. Queue generally does not allow insertion of null elements. 

Deque Interface :- 

Pronounced as Deck. It is a double-ended-queue. A deque is a linear collection of elements that supports the insertion and removal of elements at both end points. 

SortedSet Interface :-

A SortedSet is a sub-interface of Set. So, A SortedSet is a set maintains elements in natural Sorting order (ascending order) or according to Comparator. 

2-Dimensional Collection :- In 2-D Collection data is organised in (key,value) pair or rows and column.

Map Interface :-

Map is an interface whose object maps  key to values. Each key can map to at most one value.Map does not allow duplicate keys. The elements will be displayed in the same order in which you put them in. Map implementations are HashMap, TreeMap, LinkedHashMap , their behavior and performance are analogous to HashSet, TreeSet and LinedHashSet.

SortedMap Interfacae :-

SortedMap is similar to SortedSet but it 2-Dimensional (organizes the data into key,value form). It maintains the entries in ascending order, according to keys' natural sorting order or according to Comparator.




That is all for today guys! i'll write more on Collections. Feel free to write any comments. I am open to any correction as we all are in the journey of learning together. 

Comments

Post a Comment