Basic TreeMap
Operations
package
com.java2novice.treemap;
import java.util.TreeMap;
public class MyBasicOperations
{
public static void main(String a[])
{
TreeMap hm
= new TreeMap();
hm.put("first",
"First inserted");
hm.put("second",
"Second inserted");
hm.put("third","Third
inserted");
System.out.println(hm);
System.out.println("Value
of second: "+hm.get("second"));
System.out.println("Is
TreeMap empty? "+hm.isEmpty());
hm.remove("third");
System.out.println(hm);
System.out.println("Size of
the TreeMap: "+hm.size());
}
}
How to iterate
through TreeMap
package com.java2novice.treemap;
import java.util.Set;
import java.util.TreeMap;
public class MyTreeMapRead
{
public static void main(String a[])
{
TreeMap hm = new TreeMap();
hm.put("first", "First inserted");
hm.put("second", "Second inserted");
hm.put("third","Third inserted");
System.out.println(hm);
Set keys = hm.keySet();
for(String key: keys)
{
System.out.println("Value of "+key+" is: "+hm.get(key));
}
}
}
How to copy Map
content to another TreeMap
package com.java2novice.treemap;
import java.util.TreeMap;
public class MyTreeMapCopy
{
public static void main(String a[])
{
TreeMap hm = new TreeMap();
hm.put("first", "First inserted");
hm.put("second", "Second inserted");
hm.put("third","Third inserted");
System.out.println(hm);
TreeMap subMap = new TreeMap();
subMap.put("s1", "S1 Value");
subMap.put("s2", "S2 Value");
hm.putAll(subMap);
System.out.println(hm);
}
}
No comments:
Post a Comment