🎓 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
In Java 21, the LinkedHashSet class introduced the addLast(E e) method, allowing elements to be added as the last element of the collection. This guide will cover the method's usage, explain how it works, and provide examples to demonstrate its functionality using animal names.
Table of Contents
- Introduction
addLastMethod Syntax- Examples
- Adding an Element as the Last Element in LinkedHashSet
- Maintaining Insertion Order with
addLast
- Real-World Use Case
- Use Case: Task Management System
- Conclusion
Introduction
The LinkedHashSet.addLast(E e) method is a new addition in Java 21 that allows you to add an element as the last element of the LinkedHashSet. This method helps maintain the desired insertion order by explicitly adding elements to the end of the set.
addLast() Method Syntax
The syntax for the addLast method is as follows:
public void addLast(E e)
- The method takes a single parameter
eof typeE, which represents the element to be added to theLinkedHashSet. - The method does not return any value.
Examples
Adding an Element as the Last Element in LinkedHashSet
The addLast method can be used to add an element at the end of a LinkedHashSet.
Example
import java.util.LinkedHashSet;
public class AddLastExample {
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");
// Adding an element as the last element
animals.addLast("Giraffe");
// Printing the LinkedHashSet
System.out.println("LinkedHashSet after addLast: " + animals);
}
}
Output:
LinkedHashSet after addLast: [Lion, Tiger, Elephant, Giraffe]
Maintaining Insertion Order with addLast
When you use addLast, the new element is added to the end of the set, maintaining the insertion order.
Example
import java.util.LinkedHashSet;
public class AddLastOrderExample {
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");
// Adding an element as the last element
animals.addLast("Giraffe");
// Adding another element as the last element
animals.addLast("Zebra");
// Printing the LinkedHashSet
System.out.println("LinkedHashSet after multiple addLast: " + animals);
}
}
Output:
LinkedHashSet after multiple addLast: [Lion, Tiger, Giraffe, Zebra]
Real-World Use Case
Use Case: Task Management System
In a task management system, tasks are typically added in the order they are created. However, there are scenarios where a task might need to be added specifically to the end of the list. The addLast method can be used to achieve this functionality.
Example
import java.util.LinkedHashSet;
public class TaskManagementSystem {
public static void main(String[] args) {
// Creating a LinkedHashSet to store tasks
LinkedHashSet<String> tasks = new LinkedHashSet<>();
// Adding initial tasks
tasks.add("Complete project report");
tasks.add("Email client updates");
tasks.add("Prepare presentation");
// Adding a low-priority task at the end
tasks.addLast("Review team feedback");
// Printing the tasks in order of priority
System.out.println("Tasks in order of priority: " + tasks);
}
}
Output:
Tasks in order of priority: [Complete project report, Email client updates, Prepare presentation, Review team feedback]
Conclusion
The LinkedHashSet.addLast(E e) method introduced in Java 21 provides a way to add elements at the end of a LinkedHashSet. By understanding how to use this method, you can efficiently manage the order of elements in your collections. This method ensures that you can explicitly control the insertion order, making it a valuable tool for collection management in your Java applications. The real-world use case of a task management system illustrates the practical application of this method in managing tasks with varying priorities.
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