JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Checkout for Promo Codes

Program: How to get sorted sub-map from TreeMap?


Description:

Below example shows how to get range of elements from sorted 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 keys are sorted, you can call subMap() method to get range of elements based on key.


Code:
package com.java2novice.treemap;

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

public class MySubTreeMap {
	
	public static void main(String a[]){
		//the treemap sorts by key
		TreeMap<String, String> hm = new TreeMap<String, String>(new MyCompr());
		//add key-value pair to TreeMap
		hm.put("java", "language");
		hm.put("computer", "machine");
		hm.put("india","country");
		hm.put("mango","fruit");
		hm.put("game","cricket");
		System.out.println("TreeMap Entries:");
		System.out.println(hm);
		Map<String, String> subMap1 = hm.subMap("game", "java");
		System.out.println("Sub-Map enties:");
		System.out.println(subMap1);
		//how to get lower boundary also part of sub-map
		Map<String, String> subMap2 = hm.subMap("game", true, "java", true);
		System.out.println("Sub-Map enties:");
		System.out.println(subMap2);
		//how to omit upper boundary in the sub-map
		Map<String, String> subMap3 = hm.subMap("game", false, "java", true);
		System.out.println("Sub-Map enties:");
		System.out.println(subMap3);
	}
}

class MyCompr implements Comparator<String>{

	@Override
	public int compare(String str1, String str2) {
		return str1.compareTo(str2);
	}
	
}

Output:
TreeMap Entries:
{computer=machine, game=cricket, india=country, java=language, mango=fruit}
Sub-Map enties:
{game=cricket, india=country}
Sub-Map enties:
{game=cricket, india=country, java=language}
Sub-Map enties:
{india=country, java=language}
<< 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
String Vs StringBuffer
We know that String is immutable object. We can not change the value of a String object once it is initiated. If we try to change the value of the existing String object then it creates new object rather than changing the value of the existing object. So incase, we are going to do more modificatios on String, then use StringBuffer. StringBuffer updates the existing objects value, rather creating new object.
Famous Quotations
Insanity: doing the same thing over and over again and expecting different results.
-- Albert Einstein

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.