Thursday, December 22, 2011

How to implement a TreeMap


java.util.TreeMap is an implementation of the java.util.SortedMap interface. This class guarantees that the map will be in ascending key order, sorted according to the natural order for the key's class (see Comparable), or by the comparator provided at creation time, depending on which constructor is used.

Following is a simple example of inserting employees into a map based on their countries. The example consists of 2 classes.

Employee.java

package com.soft.model;

public class Employee{
private String name;
private String department;
private int age;
public String getName() {
            return name;
}
public void setName(String name) {
            this.name = name;
}
public String getDepartment() {
            return department;
}
public void setDepartment(String department) {
            this.department = department;
}
public int getAge() {
            return age;
}
public void setAge(int age) {
            this.age = age;
}

}

TreeMapExample.java

package com.soft.examples;

import java.util.Collections;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;

import com.soft.model.Employee;

public class TreeMapExample {
            public static void main(String[] args) {
SortedMap<String, Employee> map = new TreeMap<String, Employee>();
SortedMap<String, Employee> descendingMap = new TreeMap<String, Employee>(Collections.reverseOrder());
Employee e = new Employee();
e.setName("Danial Fritz");
e.setDepartment("Field Based Development");
e.setAge(36);
map.put("Canada", e);
descendingMap.put("Canada", e);
e = new Employee();
e.setName("Denis Khoo");
e.setDepartment("Project Management");
e.setAge(30);
map.put("England", e);
descendingMap.put("England", e);
e = new Employee();
e.setName("Joe Kum");
e.setDepartment("Adminstration");
e.setAge(40);
map.put("Pakistan", e);
descendingMap.put("Pakistan", e);
e = new Employee();
e.setName("Anil Kumar");
e.setDepartment("Information Technology");
e.setAge(26);
map.put("China", e);
descendingMap.put("China", e);
e = new Employee();
e.setName("Anil Kumar");
e.setDepartment("Information Technology");
e.setAge(26);
map.put("Brazil", e);
descendingMap.put("Brazil", e);
e = new Employee();
e.setName("Anil Kumar");
e.setDepartment("Information Technology");
e.setAge(25);
map.put("Kowait", e);
descendingMap.put("Kowait", e);
e = new Employee();
e.setName("Anil Kumar");
e.setDepartment("Project Management");
e.setAge(26);
map.put("Jordan", e);
descendingMap.put("Jordan", e);
System.out.println("************ Sorting By Natural Order of keys ***********");
Set<String> keySet = map.keySet();
for (String key : keySet) {
            Employee employee = map.get(key);
System.out.println(key + "-----" + employee.getName() + "-----"+ employee.getDepartment() + "-----" + employee.getAge());
}
System.out.println("*** Descending Sort By Natural Order of keys ***");
keySet = descendingMap.keySet();
for (String key : keySet) {
Employee employee = map.get(key);
System.out.println(key + "-----" + employee.getName() + "-----"
+ employee.getDepartment() + "-----" + employee.getAge());
}
}
}


Output:
************ Sorting By Natural Order of keys ***********

Brazil-----Anil Kumar-----Information Technology-----26
Canada-----Danial Fritz-----Field Based Development-----36
China-----Anil Kumar-----Information Technology-----26
England-----Denis Khoo-----Project Management-----30
Jordan-----Anil Kumar-----Project Management-----26
Kowait-----Anil Kumar-----Information Technology-----25
Pakistan-----Joe Kum-----Adminstration-----40

*** Descending Sort By Natural Order of keys ***

Pakistan-----Joe Kum-----Adminstration-----40
Kowait-----Anil Kumar-----Information Technology-----25
Jordan-----Anil Kumar-----Project Management-----26
England-----Denis Khoo-----Project Management-----30
China-----Anil Kumar-----Information Technology-----26
Canada-----Danial Fritz-----Field Based Development-----36
Brazil-----Anil Kumar-----Information Technology-----26

No comments:

Post a Comment