Collections API is the most popular and widely used utility API in the Java universe. With the advent of Functional Interfaces, Lambdas and Streams in Java 8, Collections API has also undergone changes to accomodate and build-upon the newly introduced functional programming features. The most widely used Collection classes and interfaces such as Iterable and Iterator, Collection, List and Map have all been enhanced in JDK 1.8 with new features and methods.
This is the 1st article in a 4 part article series in which I will cover the changes introduced in Java 8 Collections in detail.
In this part 1 of 4, I will be covering the new default method named
forEach() introduced in
java.lang.Iterable interface with examples. This will be followed by understanding the new default implementation of Iterator interface’s
remove() method, and how it makes implementing the Iterator easier than before.
New default method
forEach() added to the Iterable Interface in Java 8
Iterable interface is a commonly extended interface among the Collections interfaces as it provides the ability to iterate over the members of a collection.
Set are among the important Collections interfaces that extend
Iterable, apart from other interfaces.
Java 8’s new
Iterable.forEach() method has the following signature –
default void forEach(Consumer<? super T> action)
action is the only parameter and is an instance of a Consumer Functional InterfaceClick to read detailed tutorial on Consumer Functional Interfaces
forEach() is implemented in the
Iterable interface itself as a default methodTutorial explaining the concepts of Java 8’s new default methods.
Iterable.forEach() method ‘consumes’ all the elements of the iterable collection of elements passed to it. The logic for consumption is passed to the method as an instance of a Consumer functional interface. An important point to note is that the forEach method iterates internally over the collection of elements passed to it rather than externally. You can read about declarative internal iterators and how they differ from commonly used external iterators in this tutorial hereClick to read detailed tutorial explaining internal vs external iterators.
Let us now take a look at Java code showing how to use
Iterable.forEach() method for consuming the elements of an
Iterable collection –
intList, is created using the
forEach()method is invoked on the
intList. As we read above,
forEach()method’s default implementation from
- We pass a method referenceClick to read tutorial on Java 8 Method References to
System.out.println()method, which is a
Consumertype of function, as parameter to the
forEach()method internally iterates and consumes, or prints, the elements of
- Next we created an instance of a
intSet, by using its concrete implementation
Iterable. Hence, we are able to print the elements in
forEach()method similar to the way we did with
New default method
remove() added to
Iterator interface in Java 8
Prior to Java 8, implementing
Iterator interface without the support for
remove() method implied that the designers had to override the method and throw an
UnsupportedOperationException. Such an override was commonly done and over the years had become kind of staple in
Iterator implementations not supporting the remove operation.
With Java 8 arrived the feature of adding default implementations of methods in interfaces itself. Java designers have used this new feature and added a default implementation of the
remove() method in the
Iterator interface itself which throws
As a result, the practice of overriding the
remove() method, whenever it wasn’t supported, has now been inverted to overriding the
remove() method only when remove functionality has to be implemented. This has removed the unnecessary overhead of overriding and throwing the
UnsuportedOperationException everytime when implementing an
Iterator without remove functionality.
The default implementation of
remove() method in Java 8’s
Iterable is as shown below –
As you can see in the above code, the default implementation of
Iterator.remove() method just throws an
UnsupportedOperationException with message
In this tutorial we dipped our feet into the waters of Java 8 Collection Enhancements by taking a look at the changes in
In the forthcoming parts I will be taking you to the depths of Java 8 Collections enhancements, where I will explain the changes in important Collection classes viz.
Map interfaces. Specifically, among the
Map enhancements we will take a look at the new methods added in Java 8 which make multi-value maps handling easier than before.