HashMap TreeMap

import java.util.*;
class Student  {
private int stid; private String sname;
private double pert;
Student(int id, String s, double p) {
stid= id;
sname = s;
pert =p;
}
public int getStid() { return stid; }
public String getSname() { return sname; }
public double getPert() { return pert; }

}

public class MyMapExample
{
public static void main(String args[])
{

Student s1 = new Student(303,"Mohin",78.90);
Student s2 = new Student(203,"Dipen",88.90);
Student s3 = new Student(1003,"Jay",98.90);

HashMap<Integer,Student> hhmap = new HashMap<Integer,Student>();

hhmap.put(s1.getStid(),s1);
hhmap.put(s2.getStid(),s2);
hhmap.put(s3.getStid(),s3);
Set<Integer> ttset = hhmap.keySet();
Iterator<Integer> itto = ttset.iterator();
while (itto.hasNext() )
{
Integer i= itto.next();
System.out.print( i + " : ");
Student s = hhmap.get(i) ;
System.out.println(s.getSname() + " : " + s.getPert());


}

System.out.println(" Tree Map Example ");
//Hashmap<Integer, String> hmap = new Hashmap<Integer>();
TreeMap<Integer,String> hmap = new TreeMap<Integer,String>();

hmap.put(301,"Mohin");
hmap.put(201,"Dipen");
hmap.put(1001,"Jay");
hmap.put(101,"Kamlesh");
hmap.put(701,"Digant");
hmap.put(501,"Vishal");
hmap.put(404,"Kaushal");
//hmap.putAll(hhmap);

System.out.println("Hash Map  elements : ") ;
System.out.println("TotalSize " + hmap.size());


//System.out.println(hmap.get(404));

Set<Integer> tset = hmap.keySet();
Iterator<Integer> ito = tset.iterator();
while (ito.hasNext() )
{
Integer i= ito.next();
System.out.print( i + " : "+ hmap.get(i) + " \n");

}

hmap.remove(1001);
System.out.println("After remove");

Set<Integer> tset1 = hmap.keySet();
Iterator<Integer> ito1 = tset.iterator();
while (ito1.hasNext() )
{
Integer i= ito1.next();
System.out.print( i + " : "+ hmap.get(i) + " \n");

}

Collection<String> stname = hmap.values();

System.out.println(stname.size());

//System.out.println(hmap.size());
// Print elements of Set using Iterator object
// create iterator object on hashmap




}
}

Comments

Popular posts from this blog

Custom Exception example for BankAccount

Annotation

Interface with class and interface as member