← Back to Advanced Java Course | Chapter 5: Collections Advanced | Lesson 1 of 4

Java TreeMap और TreeSet

TreeMap और TreeSet अपने items को हमेशा sorted order में रखते हैं, जैसे एक shelf जहां किताबें अपने आप alphabetically रखी जाती हैं। वे अगले या पिछले item को ढूंढना भी आसान बनाते हैं।
Syntax
java
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

java
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
	}
}

Navigating TreeMap Methods

TreeMap NavigableMap interface implement करता है, जो किसी दी गई value के relative nearest matching key ढूंढने (जैसे floorKey या ceilingKey), या map की entries की एक sorted sub-range सीधे निकालने के लिए methods का एक भरपूर set जोड़ता है।

उदाहरण: Navigating TreeMap 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(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));
	}
}

Introduction to TreeSet

एक TreeSet किसी TreeMap का Set equivalent है: यह अपने elements को अपने आप ascending sorted order में रखता है और, किसी भी Set की तरह, duplicate entries को पूरी तरह reject करता है।

उदाहरण: Introduction to TreeSet

java
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
	}
}

Navigating TreeSet Methods

TreeSet NavigableSet interface implement करता है, वही तरह की nearest-match और sub-range query methods देते हुए जो NavigableMap, TreeMap के लिए देता है, आपको पूरे set को scan किए बिना किसी दिए गए point के आस-पास elements efficiently ढूंढने देते हुए।

उदाहरण: Navigating TreeSet Methods

java
// 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));
	}
}

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

java
// 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);
	}
}
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. किसी TreeMap में null key या natural ordering वाले TreeSet में null जोड़ना, जो NullPointerException फेंकता है।
  2. ऐसे objects store करना जो Comparable implement नहीं करते (और कोई Comparator न देना), जो पहले insert पर ClassCastException फेंकता है।
  3. ऐसा Comparator उपयोग करना जो अलग objects के लिए 0 return करे, जिससे एक TreeSet उन्हें duplicates मानकर चुपचाप एक को हटा देता है।
🔒

Chapter Quiz — Complete all 4 topics to unlock

0/4 topics done

Complete these topics first:

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.