30 Nov 2013

sorting map in java example code

//map in java example code (using enumeration and iterator):

public class mapexample
{
public static void main(String vals[])
{
try
{
//creating vector object
java.util.Vector v=new java.util.Vector();

//adding elements to vector
int primitive=10;
Integer wrapper=new Integer(20);
String str="Satya";
v.add(primitive);
v.add(wrapper);
v.add(str);
v.add(2,new Integer(30));

System.out.println("the items in vector are: +v");
System.out.println("the number of items in vector are (size): "+v.size());
System.out.println("the item at position 2 is: "+v.elementAt(2));
System.out.println("the first item of vector is: "+v.firstElement());
System.out.println("the last item of vector is: "+v.lastElement());
System.out.println("now removing item at position 2");
v.removeElementAt(2);

//by using enumeration
java.util.Enumeration e=v.elements();
System.out.println("\nthe items in vector are (througth enumeration): "+v);
while(e.hasMoreElements())
{
System.out.println("the item is: "+e.nextElement());
}

//by using iterator
java.util.Iterator itr=v.iterator();
System.out.println("\nthe items in vector are (througth iterator): ");
while(itr.hasNext())
{
System.out.println(itr.next()+" ");
}
}
catch(Exception e)
{
 e.printStackTrace();
}
}
}

output:

sorting map in java example code

0 comments:

Post a Comment