TreeSet descendingIterator() in Java

descendingIterator(): This method is available in java.util.TreeSet class of Java.

Syntax:

Iterator<V> java.util.TreeSet.descendingIterator()

This method returns an iterator over the elements in this set in descending order.

Parameters: NA

Returns: an iterator over the elements in this set in descending order.

Exceptions: NA

Approach

Java

import java.util.Iterator;
import java.util.TreeSet;

public class TreeSetdescendingIterator {
    public static void main(String[] args) {

        TreeSet<String> treeSet = new TreeSet<String>();

        treeSet.add("Hello");
        treeSet.add("Java");
        treeSet.add("Program");
        treeSet.add("C++");

        Iterator<String> iterator = treeSet.descendingIterator();

        while (iterator.hasNext()) {
            System.out.print(iterator.next() + " ");

        }

    }
}

Output:

Program Java Hello C++ 

TreeSet contains(Object) in Java

contains(Object): This method is available in java.util.TreeSet class of Java.

Syntax:

boolean java.util.TreeSet.contains(Object o)

This method takes one argument. This method returns true if this set contains the specified element.

Parameters: One parameter is required for this method.

o: object to be checked for containment in this set.

Returns: true if this set contains the specified element.

Throws:

1. ClassCastException - if the specified object cannot be compared with the elements currently in the set.

2. NullPointerException - if the specified element is null and this set uses natural ordering, or its comparator does not permit null elements

Approach 1: When no exception

Java

import java.util.TreeSet;

public class TreeSetcontains {
    public static void main(String[] args) {

        TreeSet<String> treeSet = new TreeSet<String>();

        treeSet.add("Hello");
        treeSet.add("Java");
        treeSet.add("Program");
        treeSet.add("C++");

        Object o = "Java";

        System.out.println(treeSet.contains(o));

    }
}

Output:

true


Approach 2: NullPointerException 

Java

import java.util.TreeSet;

public class TreeSetcontains {
    public static void main(String[] args) {

        TreeSet<String> treeSet = new TreeSet<String>();

        treeSet.add("Hello");
        treeSet.add("Java");
        treeSet.add("Program");
        treeSet.add("C++");

        Object o = null;

        System.out.println(treeSet.contains(o));

    }
}

Output:

Exception in thread "main" java.lang.NullPointerException at java.base/java.util.Objects.requireNonNull(Objects.java:208) at java.base/java.util.TreeMap.getEntry(TreeMap.java:345) at java.base/java.util.TreeMap.containsKey(TreeMap.java:233) at java.base/java.util.TreeSet.contains(TreeSet.java:234)


TreeSet comparator() in Java

comparator(): This method is available in java.util.TreeSet class of Java.

Syntax:

Comparator<? super String> java.util.TreeSet.comparator()

This method returns the comparator used to order the elements in this set,or null if this set uses the natural ordering of its elements.

Parameters: NA

Returns: the comparator used to order the elements in this set, or null if this set uses the natural ordering of its elements.

Exceptions: NA

Approach

Java

import java.util.TreeSet;

public class TreeSetcomparator {
    public static void main(String[] args) {

        TreeSet<String> treeSet = new TreeSet<String>();

        treeSet.add("Hello");
        treeSet.add("Java");
        treeSet.add("Program");
        treeSet.add("C++");

        System.out.println(treeSet.comparator());

    }
}

Output:

null


TreeSet clone() in Java

clone(): This method is available in java.util.TreeSet class of Java.

Syntax:

Object java.util.TreeSet.clone()

This method returns a shallow copy of this TreeSet instance.

Parameters: NA

Returns: a shallow copy of this set.

Exceptions: NA

Approach

Java

import java.util.TreeSet;

public class TreeSetclone {
    public static void main(String[] args) {

        TreeSet<String> treeSet = new TreeSet<String>();

        treeSet.add("Hello");
        treeSet.add("Java");
        treeSet.add("Program");
        treeSet.add("C++");

        System.out.println(treeSet.clone());

    }
}

Output:

[C++, Hello, Java, Program]