🎓 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.add() method in Java is used to add elements to a LinkedHashSet.
Table of Contents
- Introduction
addMethod Syntax- Examples
- Adding Elements to a LinkedHashSet
- Handling Duplicate Elements
- Conclusion
Introduction
The LinkedHashSet.add() method is a member of the LinkedHashSet class in Java. It allows you to add elements to a LinkedHashSet. If the element is not already present in the set, it is added and the method returns true; otherwise, it returns false.
add() Method Syntax
The syntax for the add method is as follows:
public boolean add(E e)
- The method takes a single parameter
eof typeE, which represents the element to be added to theLinkedHashSet. - The method returns a boolean value:
trueif the element was added to the set (it was not already present).falseif the element was not added (it was already present in the set).
Examples
Adding Elements to a LinkedHashSet
The add method can be used to add elements to a LinkedHashSet.
Example
import java.util.LinkedHashSet;
public class AddExample {
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: " + animals);
}
}
Output:
LinkedHashSet: [Lion, Tiger, Elephant]
Handling Duplicate Elements
The add method returns false if the element is already present in the LinkedHashSet.
Example
import java.util.LinkedHashSet;
public class DuplicateExample {
public static void main(String[] args) {
// Creating a LinkedHashSet of Strings
LinkedHashSet<String> animals = new LinkedHashSet<>();
// Adding elements to the LinkedHashSet
boolean added1 = animals.add("Lion");
boolean added2 = animals.add("Tiger");
boolean added3 = animals.add("Lion"); // Attempt to add a duplicate
// Printing the results of adding elements
System.out.println("Added Lion first time: " + added1);
System.out.println("Added Tiger: " + added2);
System.out.println("Added Lion second time (duplicate): " + added3);
// Printing the LinkedHashSet
System.out.println("LinkedHashSet: " + animals);
}
}
Output:
Added Lion first time: true
Added Tiger: true
Added Lion second time (duplicate): false
LinkedHashSet: [Lion, Tiger]
Conclusion
The LinkedHashSet.add() method in Java provides a way to add elements to a LinkedHashSet. By understanding how to use this method, you can efficiently manage collections of unique elements in your Java applications. The method ensures that no duplicate elements are added to the set, maintaining the uniqueness of the elements.
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