Checkout for Promo Codes

Program: How to get last key element from TreeMap (Sorted Map)?


Below example shows how to get last key from TreeMap based on sorting. To get last element, you have to sort the TreeMap based on user defined objects by using comparator object. You can include you own custom sorting logic with compare method. By passing comparator object to the TreeMap, you can sort the keys based on the logic provided inside the compare method. Once the TreeMap keys are in sorting order, you can call lastKey() method to get the last key, then you can get the corresponding value object.

package com.java2novice.treemap;

import java.util.Comparator;
import java.util.Map.Entry;
import java.util.TreeMap;

public class MyTreeLastElement {

	public static void main(String a[]){
		//By using salary comparator (int comparison)
		TreeMap<Emp,String> trmap = new TreeMap<Emp, String>(new MySalaryCompr());
		trmap.put(new Emp("Ram",3000), "RAM");
		trmap.put(new Emp("John",6000), "JOHN");
		trmap.put(new Emp("Crish",2000), "CRISH");
		trmap.put(new Emp("Tom",2400), "TOM");
		Emp em = trmap.lastKey();
		System.out.println("Least salary emp: "+em);
		Entry<Emp,String> ent = trmap.lastEntry();
		System.out.println("Entry set values: ");
		System.out.println(ent.getKey()+" ==> "+ent.getValue());

class MySalaryCompr implements Comparator<Emp>{

	public int compare(Emp e1, Emp e2) {
		if(e1.getSalary() < e2.getSalary()){
			return 1;
		} else {
			return -1;

class Emp{
	private String name;
	private int salary;
	public Emp(String n, int s){ = n;
		this.salary = s;
	public String getName() {
		return name;
	public void setName(String name) { = name;
	public int getSalary() {
		return salary;
	public void setSalary(int salary) {
		this.salary = salary;
	public String toString(){
		return "Name: ""-- Salary: "+this.salary;

Least salary emp: Name: Crish-- Salary: 2000
Entry set values: 
Name: Crish-- Salary: 2000 ==> CRISH
<< Previous Program | Next Program >>

List Of All TreeMap Sample Programs:

  1. Basic TreeMap Operations.
  2. How to iterate through TreeMap?
  3. How to copy Map content to another TreeMap?
  4. How to search a key in TreeMap?
  5. How to search a value in TreeMap?
  6. How to get all keys from TreeMap?
  7. How to get entry set from TreeMap?
  8. How to delete all elements from TreeMap?
  9. How to sort keys in TreeMap by using Comparator?
  10. How to sort keys in TreeMap by using Comparator with user define objects?
  11. How to get sorted sub-map from TreeMap?
  12. How to get first key element from TreeMap (Sorted Map)?
  13. How to get last key element from TreeMap (Sorted Map)?
  14. How to reverse sorted keys in a TreeMap?
Knowledge Centre
Can we call servlet destory() from service()?
As you know, destory() is part of servlet life cycle methods, it is used to kill the servlet instance. Servlet Engine is used to call destory(). In case, if you call destory method from service(), it just execute the code written in the destory(), but it wont kill the servlet instance. destroy() will be called before killing the servlet instance by servlet engine.
Famous Quotations
Good judgment comes from experience, and experience comes from bad judgment.
-- Barry LePatner

About Author

I'm Nataraja Gootooru, programmer by profession and passionate about technologies. All examples given here are as simple as possible to help beginners. The source code is compiled and tested in my dev environment.

If you come across any mistakes or bugs, please email me to [email protected].

Most Visited Pages

Other Interesting Sites

Reference: Java™ Platform Standard Ed. 7 - API Specification | Java™ Platform Standard Ed. 8 - API Specification | Java is registered trademark of Oracle.
Privacy Policy | Copyright © 2020 by Nataraja Gootooru. All Rights Reserved.