September 25, 2017

Difference between maps in java - Hash and Tree map

/**
 *
 */
package com.belazy.misc;

import java.util.HashMap;
import java.util.TreeMap;

/**
 * @author belazy
 *
 */
public class CollectionMaps {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
CollectionMaps colMap = new CollectionMaps();
colMap.callHashMap();
colMap.callTreeMap();

}

private void callTreeMap() {
// TODO Auto-generated method stub
System.out.println("Inside Tree Map");
TreeMap<Integer, String> studentDetails = new TreeMap<Integer, String>();
studentDetails.put(10, "sachin");
studentDetails.put(7, "jadeja");
studentDetails.put(5, "dhoni");
studentDetails.put(1, "virat");
studentDetails.put(3, "saurav");
System.out.println("data in map "+studentDetails);
System.out.println("reverse order :"+studentDetails.descendingMap());
System.out.println("descending key sets : "+studentDetails.descendingKeySet());
System.out.println("first Entry "+studentDetails.firstEntry());
}

private void callHashMap() {
// TODO Auto-generated method stub
System.out.println("Inside Hash Map");
HashMap<Integer, String> studentDetails = new HashMap<Integer, String>();

studentDetails.put(10, "sachin");
studentDetails.put(7, "jadeja");
studentDetails.put(5, "dhoni");
studentDetails.put(1, "virat");
studentDetails.put(3, "saurav");

System.out.println(studentDetails);


}

}

Facebook comments