Sunday, 8 February 2015

#2. ArrayList Overview and Examples


ArrayList is an inbuilt java class available in java.util package. It implements the List interface. 

ArrayList  acts like an array, but it has extra features over array which make it very useful.

Features of ArrayList are :

1. ArrayList is growable. The size of ArrayList is not fixed as it was in array we can store as many elements as we want.when new element require to store in ArrayList and there is no place in ArrayList , it java automatically increase the size of ArrayList.

2. ArrayList can store any kind of object. ArrayList is collection of heterogeneous objects. We can store integer, double, string data in a single array list.  

Example:

1. Declare an ArrayList

We can declare an ArrayList by just using new key word.  



 2. Add elements into ArrayList

To add elements into ArrayList, we can use method add(). This is also an inbuilt method of ArrayList Class.



 You can see I added three different values into ArrayList object dummyList.

3. Access elements from ArrayList
All elements are added successfully into ArrayList. Now you can access these elements by get() method. We can access elements by using the index values of objects which start from 0 same as in array.

dummyList.get(0) will return the first object stored at index 0.


 size() method returns the number of elements situated in ArrayList.
This loop will print all the objects stored in list.

4.OutPut




You should try it by your self. I will post other uses of ArrayList in my next post.

I hope it will help you to get an overview of ArrayList.

Your valuable feedback will surely help me to improve.

Sunday, 1 February 2015

Java Collection Framework Overview

Hello Java suitors,

Java Collection framework is very easy and useful. If once you understand the basic you will start to love it. In beginning you need not to know about whole collection framework. I will make familiar you with collection framework.

Java Collections have some important interfaces you should first aware about it. There are some interface like List Interface, Set Interface, SortedSet Interface, Map Interface.



  • List Interface : You can assume it as a structure by it's help we can make a list of objects.Features of List are -

    • It is growable list means according to requirement it can grow size is not fix as it is fix in array. 
    • It allows us to store duplicate values means we can store same object as many times as we want.
    • Practically We are not going to directly use the List interface we will use ArrayList Class,Vector Class, LinkedList Class which implement the list Interface.

  • Set Interface : Set is another structure by it's help we can store objects in heap where ordering of objects does not matter.Now you are thinking that if order is not there then how we retrieve objects. Your question is fare I will tell you later. Features of Set interface are -

    • Set does not allow you to store duplicate value.
    • Order of objects does not matter.

  • SortedSet Interface : This is child interface of Sort interface.Features of SortedSet Interface are -

    • It stores objects in sorted way.

  • Map Interface : Stores objects in key-value fashion.Means to access objects you need to know key.It is same like if I know your address then I can find your house.Your house's address is key for your house.

Dear readers, I hope you got a glimpse of Java Collections framework. Don't worry about the practical use Step by Step I will demonstrate practical examples for all collections framework classes with snap shots.


Post your feedback as well as query or difficulty you are finding, I will be happy to help you more.