Collections

Java TreeSet methods Code With Examples

Table of Contents

Program

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

public class TreesetWithMethods 
{
	public static void main(String[] args) 
	{
			TreeSet<String> tree1 = new TreeSet<String>();
			tree1.add("Sun");
			tree1.add("Micro");
			tree1.add("System");
			System.out.println("TreeSet :" + tree1);
			System.out.println("");
			TreeSet<String> tree2 = new TreeSet<String>();
			tree2.add("developed");
			tree2.add("java");
			
                        //addAll​(Collection<? extends E> c)
                        tree1.addAll(tree2);
			System.out.println("1. After addAll :" + tree1);
			System.out.println("");
			
                        //remove​(Object o)
                        System.out.println("2. boolean remove 'developed' :" + tree1.remove("developed"));
			System.out.println("After Remove :" + tree1);
			System.out.println("");
			TreeSet<String> treeHeadSet = new TreeSet<String>();
			
                        //headSet​(E toElement)
                        treeHeadSet = (TreeSet) tree1.headSet("java");
			System.out.println("3. After Headset :" + treeHeadSet);
			Iterator iterator;
			
                        //iterator()
                        iterator = tree1.iterator();
			System.out.println("");
			System.out.println("4 . Iterator For Ascending");
		while (iterator.hasNext()) 
		{
				System.out.println(iterator.next() + " ");
		}
			TreeSet<String> treeReverse = new TreeSet<String>();
			treeReverse = (TreeSet) tree1.descendingSet();
			
                        //descendingIterator()
                        Iterator iterator2;
			iterator2 = treeReverse.iterator();
			System.out.println("");
			System.out.println("5 . Iterator For decending");
		while (iterator2.hasNext()) 
		{
			System.out.println(iterator2.next() + " ");
		}
	}
}

Output

 	TreeSet :[Micro, Sun, System]

	1. After addAll :[Micro, Sun, System, developed, java]

	2. boolean remove 'developed' :true
	After Remove :[Micro, Sun, System, java]

	3. After Headset :[Micro, Sun, System]

	4 . Iterator For Ascending
	Micro 
	Sun 
	System 
	java 

	5 . Iterator For decending
	java 
	System 
	Sun 
	Micro 

Description

public boolean addAll​(Collection c)

Adds all of the elements in the specified collection to this set.

Specified by:

addAll in interface Collection

Specified by:

addAll in interface Set

Overrides:

addAll in class AbstractCollection

Parameters:

c – collection containing elements to be added to this set

Returns:

true if this set changed as a result of the call

Throws:

ClassCastException – if the elements provided cannot be compared with the elements
currently in the set
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

public boolean remove​(Object o)

Removes the specified element from this set if it is present. More formally, removes an element e such
that Objects.equals(o, e), if this set contains such an element. Returns true if this set contained the
element (or equivalently, if this set changed as a result of the call). (This set will not contain the element
once the call returns.)

Specified by:

remove in interface Collection

Specified by:

remove in interface Set

Overrides:

remove in class AbstractCollection

Parameters:

o – object to be removed from this set, if present

Returns:

true if this set contained the specified element

Throws:

ClassCastException – if the specified object cannot be compared with the elements currently in this set
NullPointerException – if the specified element is null and this set uses natural ordering, or its comparator does not permit null elements

public SortedSet headSet​(E toElement)

Description copied from interface: NavigableSet
Returns a view of the portion of this set whose elements are strictly less than toElement. The returned set is backed by this set, so changes in
the returned set are reflected in this set, and vice-versa. The returned set supports all optional set operations that this set supports.
The returned set will throw an IllegalArgumentException on an attempt to insert an element outside its range.
Equivalent to headSet(toElement, false).

Specified by:

headSet in interface NavigableSet

Specified by:

headSet in interface SortedSet

Parameters:

toElement – high endpoint (exclusive) of the returned set

Returns:

a view of the portion of this set whose elements are strictly less than toElement

Throws:

ClassCastException – if toElement is not compatible with this set’s comparator (or, if the set has no comparator if toElement does not implement Comparable). Implementations may, but are not required to, throw this exception if toElement cannot be compared to elements currently in the set.
NullPointerException – if toElement is null and this set uses natural ordering, or its comparator does not permit null elements
IllegalArgumentException – if this set itself has a restricted range, and toElement lies outside the bounds of the range

public Iterator iterator()

Returns an iterator over the elements in this set in ascending order.

Specified by:

iterator in interface Collection

Specified by:

iterator in interface Iterable

Specified by:

iterator in interface NavigableSet

Specified by:

iterator in interface Set

Specified by:

iterator in class AbstractCollection

Returns:

an iterator over the elements in this set in ascending order

public Iterator descendingIterator()

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

Specified by:

descendingIterator in interface NavigableSet

Returns:

an iterator over the elements in this set in descending order