Friday, May 13, 2016

HashMap in java

package com.home;

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

public class HashMapDemo1 {
   
public static void main(String[] args) {
Map<Integer, String> a = new HashMap<Integer, String>();

a.put(1, "hari");
a.put(2, "Gopal");
a.put(3, "RAm");
a.put(4, "Mohan");
a.put(5, "Daro");

int size = a.size();

System.out.println(size);


a.remove(1);
System.out.println(a);

Set set = a.entrySet();

Iterator it = set.iterator();

while(it.hasNext()){

Map.Entry me = (Map.Entry) it.next();

System.out.println("This is key = "+me.getKey()+" : "+"This is value "+me.getValue());
}
}
}
The Output of the Following Program is:

5
{2=Gopal, 3=RAm, 4=Mohan, 5=Daro}
This is key = 2 : This is value Gopal
This is key = 3 : This is value RAm
This is key = 4 : This is value Mohan
This is key = 5 : This is value Daro

vector in java

package com.home;

import java.util.Iterator;
import java.util.Vector;

public class VectorDemo {

public static void main(String[] args) {

Vector obj = new Vector();

obj.add(12);
obj.add("ram");
obj.add(1);



//System.out.println(obj);

/* Iterator it = obj.iterator();

while(it.hasNext()){
System.out.println(it.next());
}
*/

for(int i=0;i<obj.size();i++){

System.out.println(obj.get(i));
}

}

}


The Output of the Following Program is:
12
ram
1

java arraylist

package com.home;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;



public class ArrayLIstDemo {


public static void main(String[] args) {

ArrayList<String> alist = new ArrayList<String>();

alist.add("ram");
alist.add("hari");
alist.add("gopal");
alist.add("barma");




Iterator it = alist.iterator();
while(it.hasNext()){
System.out.println(it.next());

}





}
}

The Output Of the above program is:
ram
hari
gopal
barma