🎓 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
LinkedHashSet is a collection that contains no duplicate elements and maintains the insertion order. This guide will provide examples of how to iterate over a LinkedHashSet using different methods, including detailed explanations and outputs.Table of Contents
- Introduction
- Using Enhanced For-Loop
- Using Iterator
- Using forEach Method (Java 8)
- Using Stream API (Java 8)
- Conclusion
1. Introduction
A LinkedHashSet is a part of the Java Collections Framework and is an implementation of the Set interface that maintains the order of insertion. This makes it useful for scenarios where you need a set that preserves the order in which elements were added.
2. Using Enhanced For-Loop
The enhanced for-loop (or for-each loop) provides a simple and readable way to iterate over a LinkedHashSet.
Example: Using Enhanced For-Loop
import java.util.LinkedHashSet;
import java.util.Set;
public class EnhancedForLoopExample {
public static void main(String[] args) {
Set<String> fruits = new LinkedHashSet<>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Orange");
for (String fruit : fruits) {
System.out.println(fruit);
}
}
}
Output:
Apple
Banana
Orange
3. Using Iterator
The Iterator provides a way to iterate over the elements and allows element removal during iteration if needed.
Example: Using Iterator
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Set;
public class IteratorExample {
public static void main(String[] args) {
Set<String> fruits = new LinkedHashSet<>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Orange");
Iterator<String> iterator = fruits.iterator();
while (iterator.hasNext()) {
String fruit = iterator.next();
System.out.println(fruit);
}
}
}
Output:
Apple
Banana
Orange
4. Using forEach Method (Java 8)
The forEach method is part of the Java 8 Stream API and provides a functional approach to iteration.
Example: Using forEach Method
import java.util.LinkedHashSet;
import java.util.Set;
public class ForEachMethodExample {
public static void main(String[] args) {
Set<String> fruits = new LinkedHashSet<>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Orange");
// Using forEach with lambda expression
fruits.forEach(fruit -> System.out.println(fruit));
// Using forEach with method reference
fruits.forEach(System.out::println);
}
}
Output:
Apple
Banana
Orange
5. Using Stream API (Java 8)
The Stream API provides a powerful way to process sequences of elements, including iteration.
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.stream.Collectors;
public class StreamAPIExample {
public static void main(String[] args) {
Set<String> fruits = new LinkedHashSet<>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Orange");
// Using stream and forEach
fruits.stream().forEach(fruit -> System.out.println(fruit));
// Using parallel stream and forEach
fruits.parallelStream().forEach(fruit -> System.out.println("Parallel: " + fruit));
}
}
Output:
Apple
Banana
Orange
Parallel: Apple
Parallel: Banana
Parallel: Orange
6. Conclusion
In this guide, we covered various methods to iterate over a LinkedHashSet in Java:
- Using Enhanced For-Loop: Simplifies code and improves readability.
- Using Iterator: Allows element removal during iteration.
- Using forEach Method (Java 8): Provides a functional programming approach.
- Using Stream API (Java 8): Offers powerful operations for processing sequences of elements, including parallel processing.
Each method has its own use cases and advantages. Choose the one that best fits your requirements for readability, functionality, and performance.
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