SortedSet clear() method in Java with Examples9 May 2025 | 3 min read In order to eliminate every element from a SortedSet, we'll use the clear() method. The clear() method does not delete the set; it merely removes every entry from the set. To put it another way, the clear() method is only used to empty an existing Set. SortedSet is an extension of the Set interface, which includes this method. When clean() is called, it makes sure that the set's size is 0, so removing all of its contents. When you want to return the set to its initial state without generating a new instance, you usually use this method. The set will no longer have any elements after using clear(), but it will still have all of its characteristics (including sorting order in a TreeSet). Syntax: Parameters: No parameters are required by the method. Return Value: There is no parameter required by the method. Example 1:The code provided shows how to use the clear() method in a TreeSet implementation of a SortedSet. The add() method is first used to populate the SortedSet with string entries. The TreeSet's sorting mechanism ensures that the elements are kept in their natural sequence. The set is then essentially restored to its initial state by calling the clear() method to delete every aspect from it. This illustrates how to fully empty a SortedSet while preserving its properties using the clear() method. Implementation:Output: The Initial Set is given by : [JavaTpoint, Welcome, World, the, to] The final set is given by : [] Example 2:The code shows how to utilize the clear() method with a SortedSet that is implemented in Java using a TreeSet. The add() method is used to build an empty SortedSet of integers and then fill it with elements. Because this is a TreeSet's default behavior, the set's items are automatically sorted in ascending order. The set is then made empty by calling the clear() method, which eliminates all of its elements. To demonstrate the impact of the clear() method and demonstrate that it successfully eliminates all stored elements, the set's initial and final states are reported. Implementation:Output: The Initial Set is given by : [10, 20, 30, 40, 50] The final set is given by : [] Next TopicJava Editors |
Traversing a matrix spirally entails moving through the elements in a circular pattern, starting from the top-left corner and moving to the right along the top row. Following every row or column traversal, the boundaries are adjusted, and the direction is switched, persisting until all elements...
10 min read
Titaniry operation, also known as ternary operator or conditional operator, is a shorthand notation for an if-else assertion in Java. It provides a concise way of writing easy conditional statements in Java. In this newsletter, we are able to discover the syntax of the ternary operator,...
4 min read
in Java The is thrown by an Enumeration's Element method which indicates that there are no more elements left in the enumeration. The is thrown by the following methods- Element() of Enumeration interface () of NamingEnumeration interface Element() of StringTokenizer class () of Iterator interface The is a...
2 min read
Exception handling is an essential aspect of robust programming. It enables developers to gracefully handle and recover from unexpected errors or exceptional conditions that may occur during program execution. Java, as a popular and widely-used programming language, offers powerful mechanisms for handling exceptions. One such mechanism...
5 min read
The field of computer science and programming has many interesting problems that not only challenge developers but also provide insights into efficient algorithmic solutions One such problem is the Range Addition Problem, which is often encountered in coding in various interviews, competitive design competition and real-world...
6 min read
The base time zone offset can be set to GMT using the SimpleTimeZone class's setRawOffset() function. To obtain the local time, the offset is applied to UTC. Syntax: public void setRawOffset(int offsetMillis) Parameters: The only parameter that the function takes in is offsetMillis, which gives the...
3 min read
An unreachable code or statement in Java is a common problem among Java beginners. It is a compile-time error. A lot of novice developers confuse the error with dead code- another Java-related phenomenon. Although the two are similar by manifestation, there are a slight difference between...
4 min read
The java.nio.FloatBuffer Class has a flip() function. To flip this buffer, use the FloatBuffer Class. The buffer will be truncated to the current location and then the position will be adjusted to zero as a result of flipping this buffer. Any marks that may have...
3 min read
Multiplying large integers under a large modulo is a critical problem in computer science, particularly in cryptography, number theory, and competitive programming. When dealing with large numbers, direct multiplication can cause integer overflow or lead to inefficient calculations. To tackle this, modular arithmetic is used to...
5 min read
In Java programming, handling files is a common task that developers often encounter. Whether it's reading from or writing to files, selecting specific files for processing, or managing file-related operations, having a straightforward way to interact with the file system is essential. Java's FileDialog class offers...
8 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India