Checkout for Promo Codes

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


Below example shows how to get first key from TreeMap based on sorting. To get first 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 firstKey() method to get the first key, then you can get the corresponding value object.

package com.java2novice.treemap;

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

public class MyTreeMapFirstElement {

	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.firstKey();
		System.out.println("Highest salary emp: "+em);
		Entry<Emp,String> ent = trmap.firstEntry();
		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;

Highest salary emp: Name: John-- Salary: 6000
Entry set values: 
Name: John-- Salary: 6000 ==> JOHN
<< 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
Transient and Volatile Modifiers
Transient: The transient modifier applies to variables only and it is not stored as part of its object's Persistent state. Transient variables are not serialized.

Volatile: Volatile modifier applies to variables only and it tells the compiler that the variable modified by volatile can be changed unexpectedly by other parts of the program.
Famous Quotations
The pessimist complains about the wind; the optimist expects it to change; the realist adjusts the sails.
-- William Arthur Ward

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.