TreeSet clear() in Java

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

Syntax:

void java.util.TreeSet.clear()

This method removes all of the elements from this set. The set will be empty after this call returns.

Parameters: NA

Returns: NA

Exceptions: NA

Approach

Java

import java.util.TreeSet;

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

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

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

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

    }
}

Output:

[]


TreeSet ceiling(String) in Java

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

Syntax:

K java.util.TreeSet.ceiling(K e)

This method takes one argument. This method returns the least element in this set greater than or equal to the given element, or null if there is no such element.

Parameters: One parameter is required for this method.

e: the value to match.

Returns: the least element greater than or equal to e, or null if there is no such element.

Throws:

1. ClassCastException - if the specified element 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 TreeSetceiling {
    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.ceiling("Hello"));

    }
}

Output:

Hello


Approach 2: NullPointerException

Java

import java.util.TreeSet;

public class TreeSetceiling {
    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.ceiling(null));

    }
}

Output:

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.lang.Comparable.compareTo(Object)" because "k1" is null at java.base/java.util.TreeMap.compare(TreeMap.java:1563) at java.base/java.util.TreeMap.getCeilingEntry(TreeMap.java:395) at java.base/java.util.TreeMap.ceilingKey(TreeMap.java:1024) at java.base/java.util.TreeSet.ceiling(TreeSet.java:434)


TreeSet addAll(Collection) in Java

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

Syntax:

boolean java.util.TreeSet.addAll(Collection<? extends String> c)

This method takes one argument. This method adds all of the elements in the specified collection to this set.

Parameters: One parameter is required for this method.

c: a collection containing elements to be added to this set.

Returns: true if this set changed as a result of the call.

Throws:

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

2. NullPointerException - if the specified collection is null or if any 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 TreeSetaddAll {
    public static void main(String[] args) {

        TreeSet<String> treeSet = new TreeSet<String>();
        TreeSet<String> c = new TreeSet<String>();
        c.add("Hello");
        c.add("Java");

        System.out.println(treeSet.addAll(c));
    }
}

Output:

true


Approach 2: NullPointerException 

Java

import java.util.TreeSet;

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

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

        System.out.println(treeSet.addAll(c));
    }
}

Output:

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.util.Collection.size()" because "c" is null at java.base/java.util.TreeSet.addAll(TreeSet.java:300)


TreeSet add(String) in Java

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

Syntax:

boolean java.util.TreeSet.add(String e)

This method takes one argument. This method adds the specified element to this set if it is not already present.

Parameters: One parameter is required for this method.

e: the element to be added to this set.

Returns: true if this set did not already contain the specified element.

Throws:

1. ClassCastException - if the specified object cannot be compared with the elements currently in this 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 TreeSetadd {
    public static void main(String[] args) {

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

        System.out.println(treeSet.add("Hello"));
    }
}

Output:

true


Approach 2: NullPointerException 

Java

import java.util.TreeSet;

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

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

        System.out.println(treeSet.add(null));
    }
}

Output:

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.lang.Comparable.compareTo(Object)" because "k1" is null at java.base/java.util.TreeMap.compare(TreeMap.java:1563) at java.base/java.util.TreeMap.addEntryToEmptyMap(TreeMap.java:768) at java.base/java.util.TreeMap.put(TreeMap.java:777) at java.base/java.util.TreeMap.put(TreeMap.java:534) at java.base/java.util.TreeSet.add(TreeSet.java:255)