In this lesson we will learn how to iterate sets in Java. The set interface does not provide a get() method such as the list interface for retrieving items.
So the only way to get elements out of the set can be to use the iterator() method, but this method does not return the elements of the set in any order.
Let’s see how many iteration methods are specified in Java.
There are basically three ways to iterate a sentence in Java. We can iterate with one of the following methods. You’re next:
- Using the iterator
- Improved use of hinges
- Using the forEach() function
How can I iterate with an iterator in Java?
With the iteration method we can only move in the right direction from the first to the last element. With the iterator() method we cannot move the elements in the opposite direction.
Let’s take an example program in which we iterate the elements of a set using the iterator() method. Follow all the coding steps.
Program source code 1 :
iterateSetSet package;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
public class IterateSetEx
{
public static void main(String[] args)
{
// Create a String type common object.
The parameter s = new HashSet(); // s.size() is 0.
int size = s.size();
System.out.println(size for adding elements: + size) ;
// Addition of elements to the set.
s.add(Orange); // s.size() is equal to 1.
s.add(Red); // s.size() is equal to 2.
s.add(Blue); // s.size() is equal to 3.
s.add(Yellow); // s.size() is equal to 4.
s.add(Green); // s.size() is equal to 5.
System.print.of.the.system (elements.of.the.set);System.print.of.the.system(s);
Iterator itr = s.iterator();
System.out.println;
while(itr.hasNext())
{
Object str = itr.next();
System.out.println(str);
}
}
}
A way out:
The size for adding the elements : 0
Elements of
[Red, blue, yellow, orange, green]
Iteration method
Red Blue Yellow Orange Green
During iteration we cannot add an element to the set at the position of the iterator. If we try to add an item during iteration, the JVM throws an exception called CompetitorModificationException.
The crucial point: Iteration means that the same process is repeated several times.
Let’s take an example of a program where we try to add an item during iteration and see what exception the JVM makes.
Program source code 2 :
Import java.util.HashSet ; Import
java.util.Iterator ; Import
java.util.Set ;
public class AddDemo
{
public static void main(String[] args)
{
Set= new HashSet<>() ;
set.add(Banana) ;
set.add(Orange) ;
set.add(Apple) ;
set.add(Mango) ;
Iterator itr = set.iterator();
while(itr.hasNext())
{
object str = itr.next();
System.out.println(str);
set.add(grapes); // Add element to iteration. This starts the exception to the simultaneous change.}}}}
A way out:
Apple
Main Power Exception java.util.ConcurrentModificationException
However, we can remove an item from the iterator’s position, as we do for the list iterator. Let’s create a program in which we remove an element defined in the position of the iterator during iteration. For a better understanding, please refer to the following code.
Program source code 3 :
Import java.util.HashSet ; Import
java.util.Iterator ; Import
java.util.Set ;
public class RemoveDemo
[
public static void head(String[] args)
[
Set= new HashSet<>() ;
set.add(Banana) ;
set.add(Orange) ;
set.add(Apple) ;
set.add(Mango) ;
Iterator itr = set.iterator() ;
while(itr.hasNext())
{
Object str = itr.next() ;
System.out.println(str) ;
// Remove a mango item.
if(str.equal(mango))
{
itr.remove();
}
}
System.out.println(set);
}
}
A way out:
Apple
Mango
Orange
Banana
[Apple, orange, banana].
The crucial point: The list can be iterated using the ListIterator method, but Set cannot be iterated using the ListIterator method.
How can I iterate a sentence with a loop extension?
In this chapter we will iterate the elements of the set using the improved loop function. Let’s create a program in which we iterate the elements of the set using the extended loop.
Program source code 4 :
iterateSet ;
import java.util.HashSet ;
import java.util.Set ;
public class IterateSetEx2
{
public static void main(String[] args)
{
// Create an integer Set object.
Assume s = new HashSet();// Add between even numbers from 10 to 30 as elements.for(int i = 10; i < = 30; i++){if(i % 2 == 0){s.add(i);}}System.out.println(Even numbers from 10 to 30);System.out.println(s);System.out.println(iteration using Enchanced For Loop);for(Integer it:s){System.out.println(it);}}}}.
A way out:
Even numbers from 10 to 30
[16, 18, 20, 22, 24, 10, 26, 12, 28, 14, 30] iteration
with advanced
loop 16 18 20 22 24 10 26 12 28 14 30
A set of iterations using the forEach loop in Java 1.8 ?
This is the third iteration method of set elements used for the forEach() method. It is a very simple way to iterate elements. Let’s look at an example of a program based on this method.
Program source code 5 :
iterateSet;
import java.util.HashSet;
import java.util.Set;
public class IterateSetEx3
{
public static void main(String[] args)
{
Set s = new HashSet();
s.add(‘A’) ;
s.add(‘B’) ;
s.add(‘C’) ;
s.add(‘D’) ;
s.add(‘E’) ;
System.out.println(s) ;
System.out.println(Iteration with any forEach loop in Java 1.8);s.forEach(System.out::println);}}
A way out:
A, B, C, D, E] iteration
using a Java 1.8 loop for each
A B C D E
Most important point: The forEach() method is available from Java 1.8.
We hope this manual has covered different types of example programs based on an iterative set of Java. I hope you’ve understood this and enjoyed the programming.
Thanks for reading!
Related Tags:
how to hide my wifi ptcl,how to hide modem d link,how to block wifi user pldt fibr,hiding ssid,how to hide your device on wifi,pldt fibr change ip address,how to hide wifi network tp-link,how to hide wifi network from others,how to connect to hidden wifi,how do i make my ssid visible,hiding wireless network,how to make your phone invisible on wifi,how to hide wifi network dlink,how to hide ssid in dlink dsl-2750u,how to hide wifi ptcl,enable ssid broadcast means,disable wlan interface,how to hide wifi network in dlink dsl 2750u,how to unhide wifi network on android,how to unhide wifi network tp-link,how to unhide wifi network on laptop,how to unhide wifi network tenda,how to unhide wifi network xfinity,how to unhide a router,broadcast ssid meaning,pldthomewifisettings,speed test,how to connect to hidden wifi pldt,how to hide wifi pldt home fibr 2020,how to hide pldt home prepaid wifi,pldt home wifi settings,how to turn off wifi pldt fibr,how to change wifi password pldt home fibr,how to hide wifi signal in mobile