java treemap
// sorted using natural order of keys
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
public class TreemapConstructor
{
static void ConstructorDemo()
{
// creating a Map
Map<Integer, String> hm = new HashMap<Integer, String>();
hm.put(14, "Hello");
hm.put(23, "World");
hm.put(32, "Java");
hm.put(41, "Flower");
hm.put(50, "Brackets");
// creating TreeMap using Map
TreeMap<Integer, String> tm = new TreeMap<Integer, String>(hm);
// printing TreeMap
System.out.println("TreeMap: " + tm);
}
public static void main(String[] args)
{
System.out.println("TreeMap(Map) constructor example: ");
ConstructorDemo();
}
}
// iterate treemap in java
import java.util.Map;
import java.util.TreeMap;
public class IterateTreeMap
{
public static void main(String[] args)
{
Map<String, String> tm = new TreeMap<String, String>();
tm.put("Audi", "Audi A8 L");
tm.put("BMW", "BMW 5 Series");
tm.put("Mercedes-Benz", "Mercedes-Benz V-Class");
tm.put("Bugatti", "Bugatti Chiron");
// using for-each loop iterate over TreeMap using entrySet() method
for(Map.Entry<String, String> entry : tm.entrySet())
System.out.println("[" + entry.getKey() + ", " + entry.getValue() + "]");
}
}
// java treemap comparator
import java.util.Comparator;
import java.util.NavigableMap;
import java.util.TreeMap;
public class TreemapComparatorExample
{
public static void main(String[] args)
{
NavigableMap<Integer, String> nm = new TreeMap<Integer, String>();
// populating tree map
nm.put(101, "apple");
nm.put(102, "banana");
nm.put(103, "apricot");
nm.put(104, "blackberry");
nm.put(105, "avocado");
// printing TreeMap
System.out.println("TreeMap: " + nm);
// using comparator() method
Comparator c = nm.comparator();
// print comparator value
System.out.println("Comparator value: " + c);
}
}
// tree-based map which will be sorted using Comparator ‘comp’ object.
import java.util.Comparator;
import java.util.TreeMap;
class Employee
{
int ID;
String name, address;
// Constructor
public Employee(int ID, String name, String address)
{
this.ID = ID;
this.name = name;
this.address = address;
}
// display employee details in main()
public String toString()
{
return this.ID + " " + this.name + " " + this.address;
}
}
// Comparator implementation
class SortbyID implements Comparator<Employee>
{
// sorting in ascending order of ID
public int compare(Employee a, Employee b)
{
return a.ID - b.ID;
}
}
public class TreemapConstructor
{
// second constructor
static void ConstructorDemo()
{
TreeMap<Employee, Integer> tm = new TreeMap<Employee, Integer>(new SortbyID());
tm.put(new Employee(230, "virat", "bengaluru"), 2);
tm.put(new Employee(410, "rohit", "chennai"), 3);
tm.put(new Employee(140, "dhoni", "mumbai"), 1);
System.out.println("TreeMap: " + tm);
}
public static void main(String[] args)
{
System.out.println("TreeMap(Comparator comp) example: ");
ConstructorDemo();
}
}
// example on TreeMap() constructor.
import java.util.TreeMap;
public class TreemapConstructor
{
static void ConstructorDemo()
{
// creating an empty TreeMap
TreeMap<Integer, String> tm = new TreeMap<Integer, String>();
tm.put(14, "Hello");
tm.put(23, "World");
tm.put(32, "Java");
tm.put(41, "Flower");
tm.put(50, "Brackets");
// printing TreeMap
System.out.println("TreeMap: " + tm);
}
public static void main(String[] args)
{
System.out.println("TreeMap() constructor example: ");
ConstructorDemo();
}
}
// java treemap sort by value
import java.util.Comparator;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
public class TreeMapByValue
{
public static <K, V extends Comparable<V>> Map<K, V> sortValues(final Map<K, V> m)
{
Comparator<K> com = new Comparator<K>()
{
public int compare(K k1, K k2)
{
int compare = m.get(k1).compareTo(m.get(k2));
if(compare == 0)
{
return 1;
}
else
{
return compare;
}
}
};
Map<K, V> sortedByValues = new TreeMap<K, V>(com);
sortedByValues.putAll(m);
return sortedByValues;
}
public static void main(String[] args)
{
TreeMap<String, String> fruits = new TreeMap<String, String>();
fruits.put("K1", "Jackfruit");
fruits.put("K2", "Raspberry");
fruits.put("K3", "Kiwifruit");
fruits.put("K4", "Tangerine");
fruits.put("K5", "Strawberry");
// calling sortvalues method
Map<String, String> sortedMap = sortValues(fruits);
Set set = sortedMap.entrySet();
Iterator iterate = set.iterator();
// print elements
while(iterate.hasNext())
{
Map.Entry ma = (Map.Entry)iterate.next();
System.out.print(ma.getKey() + ": ");
System.out.println(ma.getValue());
}
}
}
import java.util.Map;
import java.util.TreeMap;
public class TreeMapExample
{
public static void main(String[] args)
{
TreeMap<Integer, String> tm = new TreeMap<Integer, String>();
tm.put(1000, "Apple");
tm.put(1002, "Raspberry");
tm.put(1001, "Velvet apple");
tm.put(1003, "Banana");
for(Map.Entry obj : tm.entrySet())
{
System.out.println(obj.getKey() + " " + obj.getValue());
}
}
}
// in reverse order.
import java.util.Collections;
import java.util.Comparator;
import java.util.NavigableMap;
import java.util.TreeMap;
public class TreemapComparatorExample
{
public static void main(String[] args)
{
NavigableMap<Integer, String> nm = new TreeMap<Integer, String>(Collections.reverseOrder());
// populating tree map
nm.put(101, "apple");
nm.put(102, "banana");
nm.put(103, "apricot");
nm.put(104, "blackberry");
nm.put(105, "avocado");
// printing TreeMap
System.out.println("TreeMap: " + nm);
// using comparator() method
Comparator c = nm.comparator();
// print comparator value
System.out.println("Comparator value: " + c);
}
}
import java.util.*;
// Declare the variable using the interface of the object for flexibility.
// Non-primative data types only.
Map<Integer, String> mambo = new TreeMap<Integer, String>();
mambo.put(key, value);
// TreeMap will be sorted by key.
// Work with any comparable object as a key.
mambo.put(1, "Monica");
mambo.put(2, "Erica");
mambo.put(3, "Rita");