Advertisement
1) LinkedList() |
---|
This constructor creates an empty TreeSet to hold objects of same type. Example - |
TreeSet<String> ts= new TreeSet<String>();
2) TreeSet(Collection <? extends E>c) |
---|
This constructor creates a TreeSet initialized with the elements of collection c, with a condition, that collection c holds similar type of objects as the type declared by the TreeSet. |
TreeSet<String> ts1 = new TreeSet<String>():
TreeSet<String> ts2 = new TreeSet<String>(ts1);
Methods | Description |
---|---|
boolean add(E e) | Adds the element e to the TreeSet. |
int size() | Returns the total number of elements in an LinkedHashSet. |
E first() | Returns the first element in the TreeSet. |
E last() | Returns the last element in the TreeSet. |
E ceiling(E obj) | Searches & returns the smallest element e in TreeSet, such that, e>=obj |
E floor(E obj) | Searches & returns the greatest element e in TreeSet such that, e<=obj |
E higher(E obj) | Searches and returns an element e in TreeSet, such that e>obj |
E lower(E obj) | Searches and returns any element e in TreeSet such that e |
boolean isEmpty() | Returns true is HashSet is empty, else false. |
contains(Object o) | Checks if LinkedHashSet contains a specific Object. |
boolean remove(Object o) | Removes the first occurrence of an Object from the TreeSet, if it is present. |
Object[] toArray() | Returns an Object array containing all the elements of an TreeSet. |
Iterator |
Returns an Iterator to iterate over elements of an TreeSet. |
Advertisement
import java.util.*;
class TreeSet1
{
public static void main(String... ar)
{
TreeSet<Integer> tree= new TreeSet<Integer>();
tree.add(1);
tree.add(5);
tree.add(2);
tree.add(3);
tree.add(6);
System.out.println("Content of TreeSet after adding objects" + tree);
System.out.println("Size of TreeSet = "+ tree.size());
System.out.println("Smallest number >=4 in TreeSet : "+ tree.ceiling(4));
System.out.println("Greatest Number <=4 in TreeSet : "+tree.floor(4));
System.out.println("Number higher than 2 in TreeSet = "+tree.higher(2));
System.out.println("Number lower than 2 in TreeSet = "+tree.lower(2));
tree.remove(3);
System.out.println("Afer removing element 3, TreeSet = "+ tree);
}
}
Content of TreeSet after adding objects[1, 2, 3, 5, 6]
Size of TreeSet = 5
Smallest number >=4 in TreeSet : 5
Greatest Number <=4 in TreeSet : 3
Number higher than 2 in TreeSet = 3
Number lower than 2 in TreeSet = 1
Afer removing element 3, TreeSet = [1, 2, 5, 6]
import java.util.*;
class Comp implements Comparator
{
public int compare(String obj1, String obj2) //overriding Comparator's compare() method to give a descending ordering
{
return(obj2.compareTo(obj1)); //String's compareTo() method compares two String objects.
}
public static void main(String... ar)
{
TreeSet<String> tree= new TreeSet<String>(new Comp2()); //passing an object of implementer of Comparator Interface
tree.add("H");
tree.add("E");
tree.add("M");
tree.add("A");
tree.add("N");
System.out.println("Content of TreeSet with descending ordering" + tree);
System.out.println("Size of TreeSet = "+ tree.size());
}
}
Content of TreeSet with descending ordering[N, M, H, E, A]
Size of TreeSet = 5
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement