Friday, September 16, 2011

TreeMap Example in java


//Print the key and it's value where key is an integer and it's value is an ArrayList.
//TreeMapWithIntegerKeyAndListValues.java
import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;

public class TreeMapWithIntegerKeyAndListValues
{
public static void main(String arg[])
{
List list1 = new ArrayList();
List list2 = new ArrayList();
for (int i = 1; i < 11; i++)
{
list1.add(i);
list2.add(i);
}
TreeMap> treemap = new TreeMap>();

treemap.put(1, list2);

for (int key : treemap.keySet())
{
System.out.println("Key : " + key + " Value : " + treemap.get(key));
}
}
}

Output =>
Key : 1 Value : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

No comments:

Post a Comment