how to create various subsets within a TreeMap using the collections in java
Previous | Home | Next |
In this example we are going to create various subsets of a TreeMap.
For creating a TreeMap first of all we have to import the java.util package in which the java.util.TreeMap class is defined. In this example we have created a class named CollectionExample in which we have created a TreeMap and added some elements to it.
In the following example we have used the following methods,
SortedMap headMap(Key toKey)
This method returns a view of the portion of the current map whose keys are strictly less than toKey.
SortedMap tailMap(Key fromKey)
This method returns a view of the portion of the current map whose keys are strictly greater than or equal to fromKey.
SortedMap subMap(fromKey, toKey)
This method returns a view of the portion of this map whose keys range from fromKey(inclusive) to toKey(exclusive).
boolean isEmpty()
This method checks if the TreeMap is empty, means no keys are mapped to values.
In the following example we have created TreeMap and added some elements to it by using the put(Object key, Object value) to its object i.e. tm(in this example). Then we have used various method for creating subsets of a TreeMap.
package r4r.co.in;
import java.util.*;
public class CollectionExample {
/**
* @param args
*/
public static void main(String args[]) throws IllegalArgumentException, NullPointerException, ClassCastException
{
try
{
TreeMap<Integer, String> tm=new TreeMap<Integer, String>();
tm.put(1, "r4r"); // adding elements to the treeSet
tm.put(2, "tutorials");
tm.put(3, "development");
tm.put(4, "shashi");
tm.put(5, "s/w engineer");
System.out.println("The contents of the TreeSet are as follows: ");
Set set=tm.entrySet(); // creating a set of the contents of the TreeSet
Iterator itr=set.iterator(); // Invoking the iterator on the set
while(itr.hasNext())
{
Map.Entry<Integer, String> map = (Map.Entry<Integer, String>)itr.next(); // using the Map.Entry interface
int key=map.getKey(); // getting the key contained in the treeSet
String value=map.getValue(); // getting the values contained in the treeSet
System.out.print("KEY := "+key); // printing the key
System.out.print(","+"VALUE := "+value); // printing the value
System.out.println();
}
/* Following code shows various methods to show how can we divide a TreeSet into various
* subsets according to head to tail values
*/
SortedMap<Integer, String> set1;
set1=tm.headMap(4); // This gives the headMap subset i.e. values from first elements to the specified exclusive
System.out.println("The headMap subset contains these elements: "+set1); // displays the subset
set1=tm.headMap(4, true); // This gives the headMap subset i.e. values from first elements to the specified inclusive
System.out.println("The headMap subset containes these elements: "+set1); // displays the subset
set1=tm.tailMap(3); // This gives the tailMap subset i.e values from the element specified to the last inclusive
System.out.println("The tailMap subset contains these elements: "+set1); // displays the subset
set1=tm.tailMap(3, false); // This gives the tailMap subset i.e values from the element specified to the last inclusive
System.out.println("The tailMap subset contains these elements: "+set1); // displays the subset
set1=tm.subMap(1, true, 3, false); // This gives the subMap from key 1 to 3 in which first element is inclusive and last(exclusive)
System.out.println("The subset of the TreeMap contains these elements: "+set1); //displays the subset
set1=tm.subMap(1, true, 3, true); // this gives the subset in which both the specified elements are inclusive
System.out.println("The subset of the TreeMap contains these elements: "+set1); // displays the subset
set1=tm.subMap(1,3); // this gives the subset in which first element is inclusive and other is exclusive
System.out.println("The subset of the TreeMap contains these elements: "+set1);
set1=tm.descendingMap(); // this gives the key of the TreeMap in descending order
System.out.println("This subset contains the values the map in descending order: "+set1); // displays the set
System.out.println("This method gives the value the higher entry but less than specified value: "+tm.higherEntry(3));
System.out.println("This gives the lower entry: "+tm.lowerEntry(2));
System.out.println("The keyset in descending order is: "+tm.descendingKeySet()); // This gives the set of keys in descending order
System.out.println(tm.isEmpty()); // checks whether the TreeSet is empty
tm.clear(); // empties the TreeSet
System.out.println(tm.isEmpty()); //Checks whether the TreeSet is empty
}
catch(IllegalArgumentException e)
{
e.printStackTrace();
}
catch(NullPointerException e)
{
e.printStackTrace();
}
catch(ClassCastException e)
{
e.printStackTrace();
}
}
}
The output of the above given example is given as following :
The contents of the TreeSet are as follows:
KEY := 1,VALUE := r4r
KEY := 2,VALUE := tutorials
KEY := 3,VALUE := development
KEY := 4,VALUE := shashi
KEY := 5,VALUE := s/w engineer
The headMap subset contains these elements: {1=r4r, 2=tutorials, 3=development}
The headMap subset containes these elements: {1=r4r, 2=tutorials, 3=development, 4=shashi}
The tailMap subset contains these elements: {3=development, 4=shashi, 5=s/w engineer}
The tailMap subset contains these elements: {4=shashi, 5=s/w engineer}
The subset of the TreeMap contains these elements: {1=r4r, 2=tutorials}
The subset of the TreeMap contains these elements: {1=r4r, 2=tutorials, 3=development}
The subset of the TreeMap contains these elements: {1=r4r, 2=tutorials}
This subset contains the values the map in descending order: {5=s/w engineer, 4=shashi, 3=development, 2=tutorials, 1=r4r}
This method gives the value the higher entry but less than specified value: 4=shashi
This gives the lower entry: 1=r4r
The keyset in descending order is: [5, 4, 3, 2, 1]
false
true
Previous | Home | Next |