1. Create Java Project.
2. Create package com.demo
3. Copy and paste the code.
--** printing the elements : constructor 1 **--
key = a val = aa
key = b val = bb
key = c val = cc
key = e val = ee
--** printing the elements : constructor 2 **--
-- TreeMap with same salary having comparator --
key = Employee [name=Nupur, age=28, salary=4000.0] val = MD
key = Employee [name=Ritu, age=26, salary=10000.0] val = Manager
-- TreeMap with diff salary having comparator --
key = Employee [name=Ritu, age=35, salary=12000.0] val = Manager
key = Employee [name=Nupur, age=56, salary=13000.0] val = Assistant
key = Employee [name=Nagma, age=24, salary=15000.0] val = MD
--** printing the elements : constructor 3 **--
key = a val = A
key = b val = B
-- printing TreeMap values --
key = 1 val = AA
key = 2 val = BB
key = 3 val = CC
key = 4 val = DD
key = 6 val = GG
key = 7 val = UU
key = 9 val = FF
key = 12 val = OO
--printing first key => 1
--printing last key => 12
--poll first entry => 1=AA
--poll last entry => 12=OO
--lower key => 2
--lower entry => 3=CC
--floor key => 2
--floor entry => 4=DD
-- ceilingKey => 6
-- ceilingEntry => 4=DD
-- Print the desending treeMap --
key = 9 val = FF
key = 7 val = UU
key = 6 val = GG
key = 4 val = DD
key = 3 val = CC
key = 2 val = BB
-- Print the sorted HeadMap --
key = 2 val = BB
key = 3 val = CC
key = 4 val = DD
-- Print the sorted TailMap --
key = 6 val = GG
key = 7 val = UU
key = 9 val = FF
-- Print the sorted submap --
key = 2 val = BB
key = 3 val = CC
key = 4 val = DD
key = 6 val = GG
2. Create package com.demo
3. Copy and paste the code.
package com.demo;
import java.util.HashMap;
import java.util.Map;
import java.util.NavigableMap;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.Map.Entry;
import com.demo.model.Employee;
public class TreeMapMethodsDemo {
public static void main(String[] args) {
Map<String, String> treeMap = new TreeMap<String, String>();
treeMap.put("a", "aa");
treeMap.put("c", "cc");
treeMap.put("e", "ee");
treeMap.put("b", "bb");
System.out.println("--** printing the elements : constructor 1 **--");
for(Entry<String, String> entry : treeMap.entrySet()){
System.out.println("key = "+entry.getKey()+" val = "+entry.getValue());
}
System.out.println("--** printing the elements : constructor 2 **--");
System.out.println("-- TreeMap with same salary having comparator --");
Map<Employee,Object> treeMap2 = new TreeMap<Employee,Object>(Employee.SalaryComparator);
treeMap2.put(new Employee("Ritu", 26, 10000), "Manager");
treeMap2.put(new Employee("Nupur", 28, 4000), "Assistant");
treeMap2.put(new Employee("Nagma", 40, 4000), "MD");
for(Entry<Employee, Object> entry : treeMap2.entrySet()){
System.out.println("key = "+entry.getKey()+" val = "+entry.getValue());
}
System.out.println("-- TreeMap with diff salary having comparator --");
Map<Employee,Object> treeMap3 = new TreeMap<Employee,Object>(Employee.SalaryComparator);
treeMap3.put(new Employee("Ritu", 35, 12000), "Manager");
treeMap3.put(new Employee("Nupur", 56, 13000), "Assistant");
treeMap3.put(new Employee("Nagma", 24, 15000), "MD");
for(Entry<Employee, Object> entry3 : treeMap3.entrySet()){
System.out.println("key = "+entry3.getKey()+" val = "+entry3.getValue());
}
System.out.println("--** printing the elements : constructor 3 **--");
Map<String,String> map1 = new HashMap<String, String>();
map1.put("a", "A");
map1.put("b", "B");
Map<String,String> treeMap4 = new TreeMap<>(map1);
for(Entry<String, String> entry : treeMap4.entrySet()){
System.out.println("key = "+entry.getKey()+" val = "+entry.getValue());
}
System.out.println("-- printing TreeMap values --");
TreeMap<Integer, String> teeMap = new TreeMap<Integer, String>();
teeMap.put(1, "AA");
teeMap.put(2, "BB");
teeMap.put(3, "CC");
teeMap.put(4, "DD");
teeMap.put(6, "GG");
teeMap.put(7, "UU");
teeMap.put(9, "FF");
teeMap.put(12, "OO");
for(Entry<Integer, String> entry : teeMap.entrySet()){
System.out.println("key = "+entry.getKey()+" val = "+entry.getValue());
}
System.out.println("--printing first key => "+teeMap.firstKey());
System.out.println("--printing last key => "+teeMap.lastKey());
System.out.println("--poll first entry => "+teeMap.pollFirstEntry());
System.out.println("--poll last entry => "+teeMap.pollLastEntry());
System.out.println("--lower key => "+teeMap.lowerKey(3));
System.out.println("--lower entry => "+teeMap.lowerEntry(4));
System.out.println("--floor key => "+teeMap.floorKey(2));
System.out.println("--floor entry => "+teeMap.floorEntry(4));
System.out.println("-- ceilingKey => "+teeMap.ceilingKey(5));
System.out.println("-- ceilingEntry => "+teeMap.ceilingEntry(4));
System.out.println("-- Print the desending treeMap --");
NavigableMap<Integer, String> desTreeMap = teeMap.descendingMap();
for(Entry<Integer, String> entry : desTreeMap.entrySet()){
System.out.println("key = "+entry.getKey()+" val = "+entry.getValue());
}
System.out.println("-- Print the sorted HeadMap --");
SortedMap<Integer, String> sortedHeadMap = teeMap.headMap(6);
for(Entry<Integer, String> entry : sortedHeadMap.entrySet()){
System.out.println("key = "+entry.getKey()+" val = "+entry.getValue());
}
System.out.println("-- Print the sorted TailMap --");
SortedMap<Integer, String> sortedTailMap = teeMap.tailMap(6);
for(Entry<Integer, String> entry : sortedTailMap.entrySet()){
System.out.println("key = "+entry.getKey()+" val = "+entry.getValue());
}
System.out.println("-- Print the sorted submap --");
SortedMap<Integer, String> sortedSubMap = teeMap.subMap(2, 7);
for(Entry<Integer, String> entry : sortedSubMap.entrySet()){
System.out.println("key = "+entry.getKey()+" val = "+entry.getValue());
}
}
}
OUTPUT --** printing the elements : constructor 1 **--
key = a val = aa
key = b val = bb
key = c val = cc
key = e val = ee
--** printing the elements : constructor 2 **--
-- TreeMap with same salary having comparator --
key = Employee [name=Nupur, age=28, salary=4000.0] val = MD
key = Employee [name=Ritu, age=26, salary=10000.0] val = Manager
-- TreeMap with diff salary having comparator --
key = Employee [name=Ritu, age=35, salary=12000.0] val = Manager
key = Employee [name=Nupur, age=56, salary=13000.0] val = Assistant
key = Employee [name=Nagma, age=24, salary=15000.0] val = MD
--** printing the elements : constructor 3 **--
key = a val = A
key = b val = B
-- printing TreeMap values --
key = 1 val = AA
key = 2 val = BB
key = 3 val = CC
key = 4 val = DD
key = 6 val = GG
key = 7 val = UU
key = 9 val = FF
key = 12 val = OO
--printing first key => 1
--printing last key => 12
--poll first entry => 1=AA
--poll last entry => 12=OO
--lower key => 2
--lower entry => 3=CC
--floor key => 2
--floor entry => 4=DD
-- ceilingKey => 6
-- ceilingEntry => 4=DD
-- Print the desending treeMap --
key = 9 val = FF
key = 7 val = UU
key = 6 val = GG
key = 4 val = DD
key = 3 val = CC
key = 2 val = BB
-- Print the sorted HeadMap --
key = 2 val = BB
key = 3 val = CC
key = 4 val = DD
-- Print the sorted TailMap --
key = 6 val = GG
key = 7 val = UU
key = 9 val = FF
-- Print the sorted submap --
key = 2 val = BB
key = 3 val = CC
key = 4 val = DD
key = 6 val = GG
No comments:
Post a Comment