GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||||||||||||
GeezerVison is showing www.geeksforgeeks.org — SortedSet Interface in Java - GeeksforGeeks SortedSet Interface in JavaLast Updated : 10 Apr, 2026 The SortedSet interface is present in java.util package that extends the Set interface, which maintains unique elements in sorted order, either by natural ordering or a custom comparator.
DeclarationSince SortedSet is an interface, objects cannot be created directly. Use a class like TreeSet that implements it.
Output [10, 20, 40, 50] First element: 10 Last element: 50 Explanation:
Hierarchy of SortedSetSortedSet interface extends the Set interface. ![]() Performing Different Operations on SortedSet1. Adding ElementsTo add elements to a SortedSet, use the add() method.In a TreeSet, elements are automatically sorted in ascending order, duplicates are ignored, and null values are not allowed.
Output [A, B, C] 2. Accessing the ElementsWe can access elements using methods such as contains(), first(), last(), etc.
Output Sorted Set is: [A, B, C] Contains: D false First Value: A Last Value: C 3. Removing the ValuesThe values can be removed from the SortedSet using the remove() method.
Output Initial TreeSet: [A, B, C, D, E] After removing element: [A, C, D, E] 4. Iterating through the SortedSetThere are various ways to iterate through the SortedSet. The most famous one is to use the enhanced for loop.
Output A, B, C, D, E, Z,
Methods of SortedSet InterfaceThe following are the methods present in the SortedSet interface. Here, the “*” represents that the methods are part of the Set interface.
Comment Explore | ||||||||||||||||||||||||||||||||||||||
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |