🎓 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
HashSet.iterator() method in Java is used to obtain an iterator over the elements in the HashSet. This guide will cover the method's usage, explain how it works, and provide examples to demonstrate its functionality.Table of Contents
- Introduction
iteratorMethod Syntax- Examples
- Basic Example
- Real-World Use Case: Iterating Over a Set of Active Users
- Iterating Custom User Objects
- Conclusion
Introduction
The HashSet class in Java is part of the Java Collections Framework and implements the Set interface. A HashSet is used to store unique elements. The iterator method returns an iterator over the elements in the set. Iterators are useful for traversing collections, allowing you to process each element sequentially.
iterator() Method Syntax
The syntax for the iterator method is as follows:
public Iterator<E> iterator()
- The method does not take any parameters.
- The method returns an
Iteratorover the elements in theHashSet.
Examples
Basic Example
In this example, we'll use the iterator method to iterate over the elements in a HashSet.
Example
import java.util.HashSet;
import java.util.Iterator;
public class HashSetIteratorExample {
public static void main(String[] args) {
// Creating a HashSet of Strings
HashSet<String> set = new HashSet<>();
set.add("Java");
set.add("Python");
set.add("C");
set.add("JavaScript");
// Getting an iterator
Iterator<String> iterator = set.iterator();
// Iterating over the elements in the HashSet
System.out.println("Elements in HashSet:");
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
}
}
Output:
Elements in HashSet:
Java
C
Python
JavaScript
Real-World Use Case: Iterating Over a Set of Active Users
In a web application, you might want to iterate over a set of active users.
Example
import java.util.HashSet;
import java.util.Iterator;
public class ActiveUsersExample {
public static void main(String[] args) {
// Creating a HashSet to store active users
HashSet<String> activeUsers = new HashSet<>();
activeUsers.add("john_doe");
activeUsers.add("jane_smith");
activeUsers.add("alice_jones");
// Getting an iterator
Iterator<String> iterator = activeUsers.iterator();
// Iterating over the active users
System.out.println("Active users:");
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
}
}
Output:
Active users:
john_doe
jane_smith
alice_jones
Iterating Custom User Objects
You can also use the iterator method to iterate over a set of custom objects. In this example, we'll create a User class and iterate over a HashSet of User objects.
Example
import java.util.HashSet;
import java.util.Iterator;
class User {
private int id;
private String name;
private String email;
public User(int id, String name, String email) {
this.id = id;
this.name = name;
this.email = email;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public String getEmail() {
return email;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", email='" + email + '\'' +
'}';
}
}
public class HashSetCustomObjectsExample {
public static void main(String[] args) {
// Creating a HashSet of User objects
HashSet<User> users = new HashSet<>();
users.add(new User(1, "John Doe", "john@example.com"));
users.add(new User(2, "Jane Smith", "jane@example.com"));
users.add(new User(3, "Alice Jones", "alice@example.com"));
// Getting an iterator
Iterator<User> iterator = users.iterator();
// Iterating over the User objects in the HashSet
System.out.println("User objects in HashSet:");
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
}
}
Output:
User objects in HashSet:
User{id=1, name='John Doe', email='john@example.com'}
User{id=3, name='Alice Jones', email='alice@example.com'}
User{id=2, name='Jane Smith', email='jane@example.com'}
Conclusion
The HashSet.iterator() method in Java provides a way to iterate over the elements in a HashSet. This method is useful for traversing collections, whether they contain simple types like strings or more complex custom objects. By understanding how to use this method, you can efficiently process elements in a HashSet in your Java applications.
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