JAVA COLLECTION Framework

Well anywhere in JAVA we need a help of Collections For say to access elements , to process the elements Looking forward to Collection we will see first the class hierarchy.

Class hierarchy for Collection goes as follows


At the First level we have an interface named Iterable the next that comes below Iterable is the Collection that means Collection Extends Iterable then comes List ,Queue,Set So all the three interfaces at the Same level extends collection.

Under   List  we have ArrayList, LinkedList as a class so they implement List interface we can say Now under sets We have HashSet, LinkedHashSet.

                                       




Now Moving On to ArrayList we will see the methods of iterator and collection method in between the concept of Array List in example.


import java.util.*
public Class myCollection
{
public static Void main(String args[])
{
String s ;
myCollection()
{
}
mycollection(String s)
{
this.s=s;
}
ArrayLsit<String> lst= new ArrayList<String>();  // this is String type Arraylsit  
ArrayList<myCollection>lst2=new ArrayList<myCollection>(); // this is object type Arraylist
lst.add("A");                                                                             // takes string object
lst.add("B");
lst.add("C");
lst.add("D");
myCollection mc1= new myCollection("hii");
myCollection mc2= new myCollection("hello");
lst2.add(mc1);                                                              // we added an object to the List
lst2.add(mc2);                                  
we can also append one List to the other list such as

lst1.addAll(lst2);    //Adds one list to other list

Now for accesing the elements we can use iterator method

1. hasnext();
2.next();

hasnext(); returns boolean by checking is the element present from the Current element or not
next(); returns object that is gives the next object.

using this in programming

Iterator itr=lst1.iterate();
while(itr.hasnext())
{
String s =(String)itr.next();
}

System.out.print(s);

}
}

This was short and sweet example for ArrayList that is how to add the element and how to access the element.

Next post we will see the other examples of ArrayList and HashSet with new methods to add element in between the list or at particular index.


Comments

Popular posts from this blog

State space search / blocks world problem by heuristic approach /TIC TAC TOE

Navigation in Vaadin.

Drag and drop items from one Grid to another