🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
The LinkedHashSet.clear() method in Java is used to remove all elements from a LinkedHashSet.
Table of Contents
- Introduction
clearMethod Syntax- Examples
- Clearing Elements from a LinkedHashSet
- Checking if the LinkedHashSet is Empty
- Conclusion
Introduction
The LinkedHashSet.clear() method is a member of the LinkedHashSet class in Java. It allows you to remove all elements from a LinkedHashSet, leaving it empty.
clear() Method Syntax
The syntax for the clear method is as follows:
public void clear()
- The method does not take any parameters.
- The method does not return any value.
Examples
Clearing Elements from a LinkedHashSet
The clear method can be used to remove all elements from a LinkedHashSet.
Example
import java.util.LinkedHashSet;
public class ClearExample {
public static void main(String[] args) {
// Creating a LinkedHashSet of Strings
LinkedHashSet<String> animals = new LinkedHashSet<>();
// Adding elements to the LinkedHashSet
animals.add("Lion");
animals.add("Tiger");
animals.add("Elephant");
// Printing the LinkedHashSet
System.out.println("LinkedHashSet before clear: " + animals);
// Clearing all elements from the LinkedHashSet
animals.clear();
// Printing the LinkedHashSet after clear
System.out.println("LinkedHashSet after clear: " + animals);
}
}
Output:
LinkedHashSet before clear: [Lion, Tiger, Elephant]
LinkedHashSet after clear: []
Checking if the LinkedHashSet is Empty
After using the clear method, you can check if the LinkedHashSet is empty using the isEmpty method.
Example
import java.util.LinkedHashSet;
public class IsEmptyExample {
public static void main(String[] args) {
// Creating a LinkedHashSet of Strings
LinkedHashSet<String> animals = new LinkedHashSet<>();
// Adding elements to the LinkedHashSet
animals.add("Lion");
animals.add("Tiger");
animals.add("Elephant");
// Clearing all elements from the LinkedHashSet
animals.clear();
// Checking if the LinkedHashSet is empty
boolean isEmpty = animals.isEmpty();
// Printing the result
System.out.println("Is the LinkedHashSet empty? " + isEmpty);
}
}
Output:
Is the LinkedHashSet empty? true
Conclusion
The LinkedHashSet.clear() method in Java provides a way to remove all elements from a LinkedHashSet. By understanding how to use this method, you can efficiently manage collections by resetting them to an empty state when necessary. The method ensures that the LinkedHashSet is emptied, allowing for reuse or reinitialization.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment