interface SortedSet

Interface SortedSet is derived from Set interface. Set interface is very unique in the whole collections hierarchy. All the derived classes of Set interface like HashSet and TreeSet does not allow duplicate elements, a unique characteristic of all collections classes. The SortedSet adds one more feature of returning the elements in ascending order (known as natural order). This is equivalent to SortedMap.

The elements added to a SortedSet (its subclass TreeSet) must be able to get compared with Comparator interface (using compare() method); if unable to compare, the JVM throws ClassCastException.

Following is the interface signature

public interface SortedSet extends Set

Following are the abstract methods as defined in SortedSet interface

  1. Comparator comparator(): Returns a Comparator object associated with the SortedSet or null if the SortedSet uses natural ordering.
  2. Object first(): Returns the first (that is, lowest as elements are ordered in ascending order by default) present in the SortedSet.
  3. Object last(): Returns the last (that is, highest) present in the SortedSet.
  4. SortedSet subSet(int start, int end): Returns a SortedSet object that contains the elements from start and ending with end-1.
  5. SortedSet tailSet(Object ele): Returns a SortedSet object that contains the all the elements that are greater or equal to the element ele.

The above methods are illustrated in TreeSet program.


Do you know?

1. A frame can be created without a border.
2. Coffee cup on the frame title bar can be changed with your photo.
3. Size of the button depends on the layout manager you choose.
4. The space along the border of the frame and components inside can be adjusted.
5. Freelance drawing using AWT.
6. Components can be added without using layout manager.
7. Dialog box can be modal or modeless.
8. There exists HeadlessException in AWT.
9. You can draw a cube, cylinder etc. without having predefined methods.
10. addActionListener() is a method of Button but not ActionListener.

Why java.awt.event package is to be imported when java.awt is already imported. Does java.awt not include java.awt.event also?

3 Responses

  1. Nguyen Quan August 18, 2015 / 7:38 pm

    Hi Sir, have a mistake at “The elements added to a SortedSet (its subclass TreeSet) must be able to get compared with Comparator interface (using compareTo() method)”
    – I think using compare() method, not compareTo(). Thanks sir.

Leave a Reply

Your email address will not be published. Required fields are marked *