← Back to Core Java Course | Chapter 11: Collections | Lesson 9 of 17

Java का TreeMap

TreeMap एक Map implementation है जो अपनी key-value entries को एक self-balancing tree में store करता है, entries को हमेशा key से sorted रखते हुए।
Syntax
java
TreeMap<KeyType, ValueType> map = new TreeMap<>();
map.put(key, value);  // sorted by key
map.firstKey();

Introduction to TreeMap

TreeMap Map interface का एक implementation है जो अपनी key-value entries को एक red-black tree में store करता है, entries को हमेशा insertion या hash order के बजाय key से sorted रखते हुए।

उदाहरण: Introduction to TreeMap

java
import java.util.TreeMap;
public class Main {
	public static void main(String[] args) {
		TreeMap<String, Integer> map = new TreeMap<>(); // red-black tree, sorted by key
		map.put("banana", 1);
		map.put("apple", 2);
		System.out.println(map);
	}
}

Automatic Key Sorting

TreeMap में जोड़ी गई entries default रूप से उनकी key की natural ordering से sorted रखी जाती हैं -- numeric keys numerically sort होती हैं और String keys alphabetically sort होती हैं -- चाहे वे किस order में insert हुई हों।

उदाहरण: Automatic Key Sorting

java
import java.util.TreeMap;
public class Main {
	public static void main(String[] args) {
		TreeMap<Integer, String> map = new TreeMap<>();
		map.put(3, "c");
		map.put(1, "a");
		map.put(2, "b");
		System.out.println(map); // sorted by key: 1, 2, 3
	}
}

Adding and Removing Entries

TreeMap किसी भी दूसरे Map जैसे ही put, get, और remove operations support करता है, लेकिन हर insertion या removal underlying tree को भी re-balance करता है key order बनाए रखने के लिए, और एक मौजूदा key में डालना अब भी इसकी value overwrite करता है।

उदाहरण: Adding and Removing Entries

java
import java.util.TreeMap;
public class Main {
	public static void main(String[] args) {
		TreeMap<String, Integer> map = new TreeMap<>();
		map.put("apple", 3);
		map.put("apple", 5); // overwrites
		map.remove("apple");
		System.out.println(map);
	}
}

Navigation Methods

TreeMap एक plain Map से आगे navigation methods जोड़ता है, जैसे firstKey, lastKey, higherKey, और floorKey, जो guaranteed sort order उपयोग करके efficiently एक दी value के सापेक्ष सबसे छोटी, सबसे बड़ी, या सबसे करीब keys ढूंढते हैं।

उदाहरण: Navigation Methods

java
// Import java.util.TreeMap so it can be used by its short name
import java.util.TreeMap;
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Create a new TreeMap object and store it in map
		TreeMap<Integer, String> map = new TreeMap<>();
		map.put(1, "a"); map.put(5, "b"); map.put(10, "c");
		// Print a line to the console
		System.out.println(map.firstKey());
		// Print a line to the console
		System.out.println(map.lastKey());
		System.out.println(map.higherKey(5));
	}
}

Custom Sort Order with Comparator

एक TreeMap के constructor को एक Comparator pass करना default natural key ordering override करता है, entries को किसी भी custom rule से sort होने देते हुए, जैसे reverse order या key length जैसी एक computed property alphabetical order के बजाय।

उदाहरण: Custom Sort Order with Comparator

java
import java.util.TreeMap;
import java.util.Collections;
public class Main {
	public static void main(String[] args) {
		TreeMap<Integer, String> map = new TreeMap<>(Collections.reverseOrder());
		map.put(1, "a");
		map.put(5, "b");
		System.out.println(map); // reverse sorted
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. null को एक key के रूप में उपयोग करना, जो एक TreeMap में NullPointerException throw करता है।
  2. ऐसी keys उपयोग करना जो Comparable implement नहीं करतीं और कोई Comparator नहीं, जो ClassCastException throw करता है।
  3. TreeMap को HashMap जितना तेज़ होने की उम्मीद करना, जब operations O(log n) हैं।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.