Type s of iterator In collections

Ashok Veer | April 25, 2020 | Be the first to comment!

Types of  iterator In collections


Basically iterator means takes element one by one from the collection and We can either start iterating element from first to last or else we can start from the required position from the collection,

In java there are 4 ways of iterating element in collection
  1. Using for loop
  2. Using  Enhanced for loop
  3. Using Iterator interface
  4. Using forEach method with Lambda 

1] Using for loop:

1] Using for loop:

This is our traditional for loop for iterating the element from the specified index until the end condition satisfied.
  •       To use for loop we must know the size of the collection size, otherwise it’s very difficult to use for loop.
  •        Also collection need to be specifying the method which access index based element for use for loop strictly. Below Arraylist class specify the list.get(i) method which access index based element.
Example


public static void main(String[] args) {
           ArrayList list = new ArrayList<>();
           list.add(12);
           list.add(10);
           list.add(15);
           list.add(19);
          
           for (int i = 0; i < list.size(); i++) {
                System.out.println(list.get(i));
           }
          
     }

Output:
12
10
15

19

2] Using Enhanced for loop:

This is enhanced for loop where we can iterate the objects which are in primitive type in nature.
As compare to traditional for loop this enhanced for loop is more readable and simple.
This is from the jdk 1.5 in java which uses iterator internally for iterating the element. So basically enhanced for loop is more convenient way for the java developer for use and for understanding.

Example:

public static void main(String[] args) {
           ArrayList list = new ArrayList<>();
           list.add(12);
           list.add(10);
           list.add(15);
           list.add(19);
          
           for (Integer num : list) {
                System.out.println(num);
           }
}
Output:
12
10
15
19

3] Using Iterator interface:

public interface Iterator { … }

From jdk1.2 Iterator interface is present in java.

Previous explained for loop and enhanced for loop  has some limitations and problems for iterating so the iterator comes with build in method for more handy for java programmer.

Iterator interface methods are used to iterate all kind of collections. It has 2 methods for the operation of collection.
·        boolean hasNext() It will return true only if collection has more element to iterate.
·        E next() - It will return the current object present in collection.

Example

public static void main(String[] args) {
           ArrayList list = new ArrayList<>();
           list.add(12);
           list.add(10);
           list.add(15);
           list.add(19);
          
           Iterator itr = list.iterator();
           while(itr.hasNext()){
                System.out.println(itr.next());
           }
}
Output:
12
10
15
19

4] Using forEach method with lambda expression in jdk 1.8:

This is the most wonderful feature which is introduced in Jdk1.8 as we can see in previous jdk version until 1.8 programmer need to write the logic for iterate the collections, But from now programmer just need to write what need to do in every element in iteration, no need to worry about the iteration code.

So from java 8 there is internal iteration available in forEach method.

public static void main(String[] args) {
           ArrayList list = new ArrayList<>();
           list.add(12);
           list.add(10);
           list.add(15);
           list.add(19);
          
           list.forEach(System.out::println);
}
Output:
12
10
15
19

Just Simple below line print the element which are available in collection one by one , We just care about what to do about each element which are iterating not how to iterate the collection that’s  all do for us by forEach method in jdk 1.8.

list.forEach(System.out::println);

As we mentioned lambda expression which is good feature provided in java 8 we can use it for iterating the element,

Arrow Symbol   -  >  indicate the lambda expression.

list.forEach(num -> System.out.println(num+10));

list.stream().forEach(num -> System.out.println(num+10));

Above Code looks like more simple and readable,



So just you see all the ways for iterating the collection but mostly we prefer forEach or enhanced for loop which is more common and simple amongst the all.

0 comments:

Post a Comment

 
Copyright © 2019 techfloaters • All Rights Reserved.
Template Design by Ashok Veer ( veersoft solution)