Search This Blog

Sunday, 7 December 2014

How to Add and Retrive Data from the Map in Java


import java.util.Iterator;
import java.util.Map;
import java.util.Set;

class SimpleMapProgram{
    public static void main(String[] args){
 //creating the map object of key as integer and value as String type
 
 Map<Integer,String> map=new HashMap<Integer,String>();
//adding the element into map there is put() method in java for adding the element into the Map
map.put(1,"Core Java");//1 st element added into the HashMap
map.put(2,"Adv Java");//2 st element added into the HashMap
map.put(3,"Spring");//3 st element added into the HashMap
map.put(4,""Hibernate);//4 st element added into the HashMap
map.put(5,"Struts");//5 st element added into the HashMap

//retriving the element from the Hashmap
//first the give set type object for getting the all keys of the HashMap       
       Set keyset=map.keySet();
       //here getting the Iterator object for retriving the hashmap object
 Iterator<Integer> it=keyset.iterator();

      System.out.println("HashMap Element Are As Follow");
        while(it.hasNext()){
        //getting the key
            int key=it.next();
         //getting the value of the element by passing the key as argument to the get method
            String value=map.get(key);
         //printing the key and value of the object
            System.out.println(key+"-----------"+value);
        } //while loop closed
      }//main method close
}//class close




Output:


HashMap Element Are As Follow
                       1---------Core Java
                       2---------Adv Java
                       3---------Spring
                       4---------Hibernate
                       5---------Struts

Thread life cycle in java


Thread life cycle:

The Thread life cycle means the thread is going through which state that is the Thread life.
The Thread is mainly going into the five state.
The state are as below.
  • New/born

  • Runnable

  • Running

  • Blocked/Waiting state

  • Dead state

    1)New: 

    This is the start point of the Thread.When we call the Thread class constructor that time this state is performed.

    2) Runnable:

    This state means the Thread is ready for execution when we call start method then Thread is going to Runnable state.

    3) Running:

    This is the actual state where the Thread is execution.In this state the code executed  concurrently mode.For coming in this state Thread perform number of number here check that the Thread priority and all other conditions.

    4)Blocked:

    If any reason Thread is going to blocked state.If we perform the IO operation if the Thread is waiting for the file then Thread is going to waiting state.and if we call the join,sleep method then the Thread is going to Blocked state.From the blocked state Thread always go to Runnable State not in Running state.

    4)Dead/Complete:

    After complete the Thread execution the Thread is going to Dead state.it is the last state of the Thread life cycle.If  The once Thread goes into Dead state the Then never come into Running state.

        

Monday, 1 December 2014

What is the Collection in Java


Collection:

Collection is the class in the java which can stored the Homogenous,Hetrogenous object.That object we can send throgh out the network.