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

Java का TreeSet

TreeSet एक Set implementation है जो अपने elements को एक self-balancing tree में store करता है, उन्हें हमेशा अपने आप ascending order में sorted रखते हुए।
Syntax
java
TreeSet<Type> set = new TreeSet<>();
set.add(item);  // sorted order
set.first();

Introduction to TreeSet

TreeSet Set interface का एक implementation है जो अपने elements को एक red-black tree में store करता है, एक self-balancing binary search tree, जो हर element को हमेशा अपने आप sorted order में arranged रखता है।

उदाहरण: Introduction to TreeSet

java
import java.util.TreeSet;
public class Main {
	public static void main(String[] args) {
		TreeSet<Integer> set = new TreeSet<>(); // red-black tree
		set.add(5);
		set.add(1);
		System.out.println(set);
	}
}

Automatic Sorting

TreeSet में जोड़े गए elements default रूप से उनकी natural ordering के अनुसार sorted होते हैं -- numbers numerically sort होते हैं और strings alphabetically sort होते हैं -- चाहे वे किस order में insert हुए हों।

उदाहरण: Automatic Sorting

java
import java.util.TreeSet;
public class Main {
	public static void main(String[] args) {
		TreeSet<Integer> set = new TreeSet<>();
		set.add(3);
		set.add(1);
		set.add(2);
		System.out.println(set); // sorted: 1, 2, 3
	}
}

Adding and Removing Elements

TreeSet किसी भी दूसरे Set जैसे ही add और remove operations support करता है, लेकिन हर insertion या removal underlying tree को भी re-balance करता है sorted order बनाए रखने के लिए, और duplicate values अब भी reject होती हैं।

उदाहरण: Adding and Removing Elements

java
import java.util.TreeSet;
public class Main {
	public static void main(String[] args) {
		TreeSet<Integer> set = new TreeSet<>();
		set.add(5);
		set.add(5); // duplicate ignored
		set.remove(5);
		System.out.println(set);
	}
}

Navigation Methods

TreeSet एक plain Set से आगे navigation methods जोड़ता है, जैसे first, last, higher, और lower, जो guaranteed sort order का लाभ उठाते हुए efficiently एक दी value के सापेक्ष सबसे छोटे, सबसे बड़े, या सबसे करीब elements ढूंढते हैं।

उदाहरण: Navigation 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(1); set.add(5); set.add(10);
		// Print a line to the console
		System.out.println(set.first());
		// Print a line to the console
		System.out.println(set.last());
		System.out.println(set.higher(5));
	}
}

Custom Sort Order with Comparator

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

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

java
import java.util.TreeSet;
import java.util.Collections;
public class Main {
	public static void main(String[] args) {
		TreeSet<Integer> set = new TreeSet<>(Collections.reverseOrder());
		set.add(1);
		set.add(5);
		System.out.println(set); // 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 जोड़ना, जो NullPointerException throw करता है।
  2. ऐसे objects जोड़ना जो comparable नहीं, जो ClassCastException throw करता है।
  3. Duplicates store होने की उम्मीद करना, जब वे नज़रअंदाज़ किए जाते हैं।

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.