Java TreeMap और TreeSet
In this page:
TreeMap<KeyType, ValueType> map = new TreeMap<>();
TreeSet<Type> set = new TreeSet<>(); // both keep elements sorted
Introduction to TreeMap
एक TreeMap एक Map implementation है जो अपनी keys को लगातार sorted रखता है, या तो उनके natural ordering से या आपके दिए किसी custom Comparator से। HashMap के विपरीत, किसी TreeMap को iterate करना हमेशा keys को sorted order में देखता है, थोड़ा धीमे average-case operations की कीमत पर।
उदाहरण: Introduction to TreeMap
import java.util.TreeMap;
public class Main {
public static void main(String[] args) {
TreeMap<String, Integer> map = new TreeMap<>();
map.put("banana", 2);
map.put("apple", 1);
map.put("cherry", 3);
System.out.println(map); // iterates in sorted key order
}
}
Login to try C/C++/Java/PHP code in the editor
Navigating TreeMap Methods
TreeMap NavigableMap interface implement करता है, जो किसी दी गई value के relative nearest matching key ढूंढने (जैसे floorKey या ceilingKey), या map की entries की एक sorted sub-range सीधे निकालने के लिए methods का एक भरपूर set जोड़ता है।
उदाहरण: Navigating TreeMap 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(10, "ten"); map.put(20, "twenty"); map.put(30, "thirty");
// Print a line to the console
System.out.println(map.floorKey(25));
// Print a line to the console
System.out.println(map.ceilingKey(25));
}
}
Login to try C/C++/Java/PHP code in the editor
Introduction to TreeSet
एक TreeSet किसी TreeMap का Set equivalent है: यह अपने elements को अपने आप ascending sorted order में रखता है और, किसी भी Set की तरह, duplicate entries को पूरी तरह reject करता है।
उदाहरण: Introduction to TreeSet
import java.util.TreeSet;
public class Main {
public static void main(String[] args) {
TreeSet<Integer> set = new TreeSet<>();
set.add(5); set.add(1); set.add(3); set.add(1);
System.out.println(set); // sorted, duplicates rejected
}
}
Login to try C/C++/Java/PHP code in the editor
Navigating TreeSet Methods
TreeSet NavigableSet interface implement करता है, वही तरह की nearest-match और sub-range query methods देते हुए जो NavigableMap, TreeMap के लिए देता है, आपको पूरे set को scan किए बिना किसी दिए गए point के आस-पास elements efficiently ढूंढने देते हुए।
उदाहरण: Navigating TreeSet Methods
// Import java.util.TreeSet so it can be used by its short name
import java.util.TreeSet;
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Create a new TreeSet object and store it in set
TreeSet<Integer> set = new TreeSet<>();
set.add(10); set.add(20); set.add(30);
// Print a line to the console
System.out.println(set.higher(15));
// Print a line to the console
System.out.println(set.headSet(25));
}
}
Login to try C/C++/Java/PHP code in the editor
Custom Sorting in Tree Collections
आप बनते समय इसके constructor में एक custom Comparator पास करके किसी भी Tree collection में elements के ordered होने के तरीके को customize कर सकते हैं, elements के natural ordering को पूरी तरह override करते हुए, जो तब ज़रूरी है जब element type Comparable implement नहीं करता या आप इसके default order से अलग order चाहते हों।
उदाहरण: Custom Sorting in Tree Collections
// Import java.util.TreeSet so it can be used by its short name
import java.util.TreeSet;
// Import java.util.Comparator so it can be used by its short name
import java.util.Comparator;
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Create a new TreeSet object and store it in set
TreeSet<String> set = new TreeSet<>(Comparator.reverseOrder());
set.add("apple"); set.add("banana"); set.add("cherry");
// Print a line to the console
System.out.println(set);
}
}
Login to try C/C++/Java/PHP code in the editor
- किसी
TreeMapमेंnullkey या natural ordering वालेTreeSetमेंnullजोड़ना, जोNullPointerExceptionफेंकता है। - ऐसे objects store करना जो
Comparableimplement नहीं करते (और कोई Comparator न देना), जो पहले insert परClassCastExceptionफेंकता है। - ऐसा Comparator उपयोग करना जो अलग objects के लिए 0 return करे, जिससे एक
TreeSetउन्हें duplicates मानकर चुपचाप एक को हटा देता है।
Chapter Quiz — Complete all 4 topics to unlock
0/4 topics done
Complete these topics first: