This is the 16th day of my participation in the More text Challenge. For more details, see more text Challenge

Use a lambda expression to traverse the collection

Java8 adds a default forEach(Consumer Action) method to the Iterable interface, which takes a function interface as an argument. The Iterable interface is the parent interface of the Collection interface, so the Collection can also call the forEach method directly.

Because forEach(Consumer Action) requires a functional interface, it is possible to use Lambda expressions to traverse the collection elements. The following cases:

public class Test { public static void main(String[] args) { Collection c = new ArrayList(); c.add(1); c.add(2); c.add(3); c.add(4); c.forEach(obj -> System.out.println(obj)); }}Copy the code

The program calls accept(T T), the only abstract method in Consumer.

Use an Iterator to traverse the elements of the collection

Objects of the Iterator interface are also called iterators.

The Iterator interface contains the following methods:

Boolean hasNext(): return true if the iterator has not traversed all the elements in the collection. Object next(): return the next element in the collection. Void Remove (): removes the elements returned by the last next method in the collection. Void forEachRemaining(Consumer Action): This method uses a Lambda expression to traverse the elements of the collectionCopy the code

Here are some examples:

public class Test { public static void main(String[] args) { Collection c = new ArrayList(); c.add(1); c.add(2); c.add(3); c.add(4); Iterator iter = c.iterator(); While (it.hasnext ()){// Next returns an element of type object, so it needs to be cast int num = (Integer) it.next (); if(num==2){ iter.remove(); } System.out.println(num); } system.out.println (" the set after deletion is "+c); }}Copy the code

Use a Lambda expression to traverse the Iterator

Use the new Iterator forEachRemaining(Consumer Action) to iterate through the collection. The parameters of this method are also functional interfaces, so you can use Lambda expressions.

Here are some examples:

public class Test { public static void main(String[] args) { Collection c = new ArrayList(); c.add(1); c.add(2); c.add(3); c.add(4); Iterator iter = c.iterator(); iter.forEachRemaining(obj -> System.out.println(obj)); }}Copy the code

Use a foreach loop to iterate through the collection elements

Foreach is similar to Iterator in that the loop variable in foreach is not the collection element itself, but the value of the collection element. Changing this value does not affect the collection elements. And cannot be modified using the foreach collection elements, otherwise it will cause abnormal ConcurrentModinficationException.

Here are some examples:

public class Test { public static void main(String[] args) { Collection c = new ArrayList(); c.add(1); c.add(2); c.add(3); c.add(4); for(Object i : c){ int num = (Integer)i; System.out.println(num); If (num==2){if(num==2){ }}}}Copy the code

The above is my interface to Iterator some shallow view, if there is insufficient or wrong place, welcome comments to correct.