DEV Community

velvizhi Muthu
velvizhi Muthu

Posted on

Module 5-TreeSet

  1. What is TreeSet? Constructs a new, empty tree set.
  2. TreeSet is a class
  3. TreeSet is a Java.util package.
  4. TreeSet contains unique elements only.
  5. TreeSet doesn't allows null value.
  6. TreeSet does not allows duplicate elements.
  7. Elements are stored in ascending order by default.
  8. TreeSet implements NavigableSet.
  9. it is slower than HashSet and LinkedHashSet.
  10. We should mention Type Safety in TreeSet.

02.When to Use TreeSet?
You want to maintain elements in sorted order.
You need to perform range-based operations.
You don't care about insertion order, only sorted access.

03.Advantages of TreeSet?
1.Sorted Order
2.No Duplicates

04.Disadvantages of TreeSet?
1.Slower than HashSet
2.No null Elements
3.Comparable/Comparator
4.More Memory Overhead
5.Complex Sorting Logic

05.What are the method of TreeSet?
1.Add ()
Adds the specified element to this set if it is not already present.
Boolean modified and type.

2.addAll()
The method addAll(Collection) in the type TreeSet is not applicable for the arguments (int)

3.Clear()
Removes all of the elements from this set.

4.Clone()
Returns a shallow copy of this HashSet instance, the elements themselves are not cloned.

5.Contain()
Returns true if this set contains the specified element.

5.isEmpty()
Returns true if this set contains no elements.

6.remove(Object o)
The method remove(Object) in the type TreeSet is not applicable for the arguments ()

7.removeFirst()
Removed the first elements in ascending order.

8.removeLast()
Removed the last element in ascending order.

9.size()
number of elements not showed in this set.

General:
Java 100% Object oritented programming?
99.9%

Exception:
TreeSet tree=new TreeSet();
tree.add(40);
tree.add(10);
tree.add(30);
tree.add("hello");
System.out.println(tree);

java.lang.ClasscastException

Top comments (0)