Java का TreeMap
In this page:
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
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);
}
}
Login to try C/C++/Java/PHP code in the editor
Automatic Key Sorting
TreeMap में जोड़ी गई entries default रूप से उनकी key की natural ordering से sorted रखी जाती हैं -- numeric keys numerically sort होती हैं और String keys alphabetically sort होती हैं -- चाहे वे किस order में insert हुई हों।
उदाहरण: Automatic Key Sorting
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
}
}
Login to try C/C++/Java/PHP code in the editor
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
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);
}
}
Login to try C/C++/Java/PHP code in the editor
Navigation Methods
TreeMap एक plain Map से आगे navigation methods जोड़ता है, जैसे firstKey, lastKey, higherKey, और floorKey, जो guaranteed sort order उपयोग करके efficiently एक दी value के सापेक्ष सबसे छोटी, सबसे बड़ी, या सबसे करीब keys ढूंढते हैं।
उदाहरण: Navigation Methods
// 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));
}
}
Login to try C/C++/Java/PHP code in the editor
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
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
}
}
Login to try C/C++/Java/PHP code in the editor
nullको एक key के रूप में उपयोग करना, जो एकTreeMapमेंNullPointerExceptionthrow करता है।- ऐसी keys उपयोग करना जो
Comparableimplement नहीं करतीं और कोईComparatorनहीं, जोClassCastExceptionthrow करता है। TreeMapकोHashMapजितना तेज़ होने की उम्मीद करना, जब operations O(log n) हैं।
Chapter Quiz — Complete all 17 topics to unlock
0/17 topics done
Complete these topics first:
- Java Data Structures
- Java Collections Introduction
- Java List Interface
- Java ArrayList
- Java LinkedList
- Java Map Interface
- Java HashMap
- Java Set Interface
- Java TreeMap
- Java LinkedHashMap
- Java HashSet
- Java TreeSet
- Java LinkedHashSet
- Java Stack & Queue
- Java Iterator
- Java Collections Class
- Java List Sorting