Arraylist.iterator: In the previous article we have discussed about Java ArrayList trimToSize() Method with Example
In this article we are going to see the use Java ArrayList iterator() method along with suitable examples.
Java ArrayList iterator() Method with Example
iterator():
Arraylist iterator java: This java.util.ArrayList.iterator()
method is used to get an iterator to retrieve each element of the ArrayList in a proper order.
Syntax:
arrayListName.iterator()
Where,
arrayListName
refers to the name of your ArrayList.
Let’s see different examples to understand it more clearly.
Method-1: Java ArrayList iterator() Method – Example with String Type ArrayList
Approach:
- Create a new ArrayList of type String.
- Add string elements into the ArrayList using the add() method.
- Display the ArrayList elements.
- Create a variable of Iterator and store the value returned by
iterator()
method. - Use a while loop through ArrayList till it has all elements.
- Inside
iterator()
method there is some inbuild methods likenext()
to access elements. - Print the arrayList.
Program:
import java.util.*; public class Main { public static void main(String[] args) { // Create an ArrayList of string datatype ArrayList<String> arr1 = new ArrayList<String>(); // Adding some elements to the ArrayList arr1.add("vivo"); arr1.add("htc"); arr1.add("samsung"); arr1.add("realme"); arr1.add("nokia"); // Prints the ArrayList elements System.out.println("The elements in the ArrayList are: "+arr1); // Create a variable of Iterator and store the value returned by iterator() method Iterator<String> iterate = arr1.iterator(); System.out.print("ArrayList: "); // loop through ArrayList till it has all elements while(iterate.hasNext()) { // Use methods of Iterator to access elements System.out.print(iterate.next()); System.out.print(" "); } } }
Output: The elements in the ArrayList are: [vivo, htc, samsung, realme, nokia] ArrayList: vivo htc samsung realme nokia
Method-2: Java ArrayList iterator() Method – Example with Integer Type ArrayList
Approach:
- Create a new ArrayList of type Integer.
- Add Integer elements into the ArrayList using the add() method.
- Display the ArrayList elements.
- Create a variable of Iterator and store the value returned by
iterator()
- Use a while loop through ArrayList till it has all elements.
- Inside
iterator()
method there is some inbuild methods likenext()
to access elements. - Now using if condition we will filter the elements and remove those elements which are less than 20 from the arrayList.
- Print the arrayList.
Program:
import java.util.*; public class Main { public static void main(String[] args) { // Create an ArrayList of Integer datatype ArrayList<Integer> arr1 = new ArrayList<Integer>(); // Adding some elements to the ArrayList arr1.add(1); arr1.add(100); arr1.add(84); arr1.add(17); arr1.add(0); // Prints the ArrayList elements System.out.println("The elements in the ArrayList are: "+arr1); // Create a variable of Iterator and store the iterator returned by iterator() Iterator<Integer> iterate = arr1.iterator(); System.out.print("ArrayList: "); // loop through ArrayList till it has all elements while(iterate.hasNext()) { // Use methods of Iterator to access elements Integer i = iterate.next(); if(i < 20) iterate.remove(); } System.out.println(arr1); } }
Output: The elements in the ArrayList are: [1, 100, 84, 17, 0] ArrayList: [100, 84]
The best and excellent way to learn a java programming language is by practicing Simple Java Program Examples as it includes basic to advanced levels of concepts.
Related Java Programs: