English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Java Basic Tutorial

Java Flow Control

Java Array

Java Object-Oriented (I)

Java Object-Oriented (II)

Java Object-Oriented (III)

Java Exception Handling

Java List (List)

Java Queue (queue)

Java Map collection

Java Set collection

Java Input Output (I/O)

Java Reader/Writer

Java other topics

Methods for Java program to traverse Set collection

    Java Examples Comprehensive

In this example, we will learn how to iterate over the elements of a set collection in Java.

To understand this example, you should understand the followingJava programmingTopic:

Example1Use forEach loop to traverse Set

import java.util.Set;
import java.util.HashSet;
class Main {
  public static void main(String[] args) {
    //Create a set collection
    Set<String> languages = new HashSet<>();
    languages.add("Java");
    languages.add("JavaScript");
    languages.add("Python");
    System.out.println("Set: "); + languages);
    //Use forEach loop 
    System.out.println("Iterating over Set using for-foreach loop:")
    for(String language : languages) {
      System.out.print(language);
      System.out.print(", ");
    }
  }
}

Output Result

Set: [Java, JavaScript, Python]
Iterating over Set using for-foreach loop:
Java, JavaScript, Python,

In the above example, we created a collection using the HashSet class. Here, we use for-Use a foreach loop to iterate over each element in the collection.

Example2: Use iterator() to traverse the Set

import java.util.Set;
import java.util.HashSet;
import java.util.Iterator;
class Main {
  public static void main(String[] args) {
    //Create a set collection
    Set<Integer> numbers = new HashSet<>();
    numbers.add(1);
    numbers.add(3);
    numbers.add(2);
    System.out.println("Set: "); + numbers);
    //Create an Iterator instance
    Iterator<Integer> iterate = numbers.iterator();
    System.out.println("Traverse Set:");
    while(iterate.hasNext()) {
      System.out.print(iterate.next(), + ", ");
    }
  }
}

Output Result

Set: [1, 2, 3]
Traverse Set:
1, 2, 3,

In the above example, we used the HashSet class to create a collection. We used the iterator() method to iterate over the collection. Here,

  • hasNext() - Returns true if there is a next element in the collection

  • next() - Return the next element of the collection

Example3: Use the forEach() method to traverse the Set

import java.util.Set;
import java.util.HashSet;
class Main {
  public static void main(String[] args) {
    // Create a Set collection
    Set<Integer> numbers = new HashSet<>();
    //Add Element to HashSet
    numbers.add(1);
    numbers.add(2);
    numbers.add(3);
    numbers.add(4);
    System.out.println("Set: "); + numbers);
    //Iterate Over Each Element in the Collection
    System.out.print("Element of Set: ");
    // Access Each Element Using forEach() Method
    // Passing Lambda Expression to forEach()
    numbers.forEach((e -> {
      System.out.print(e + " ");
    });
  }
}

Output Result

Set: [1, 2, 3, 4]
Element of Set: 1 2 3 4

In the above example, we created a collection named numbers using the HashSet class. Note the code,

numbers.forEach((e -> {
  System.out.print(e + " ");
});

Here, we use the forEach() method to access each element in the collection. This method takes a lambda expression as a parameter. For more information about lambda expressions, please visitJava Lambda Expressions.

Java Examples Comprehensive