java 1.8
java 1.8
default methods: Java provides a facility to create default methods inside the interface. Methods which are defined
inside the interface and tagged with default are known as default methods. These methods are non-abstract methods.
=================================================
Java forEach loop
Java provides a new method forEach() to iterate the elements. It is defined in Iterable and
Stream interface. It is a default method defined in the Iterable interface. Collection classes
which extends Iterable interface can use forEach loop to iterate elements.
This method takes a single parameter which is a functional interface. So, you can pass
lambda expression as an argument.
import java.util.ArrayList;
import java.util.List;
1) map() takes a Stream and transform it to another Stream. It applies a function on each element of
Stream and store return value into new Stream. It does not flatten the stream. But flatMap() is the
combination of a map and a flat operation i.e, it applies a function to elements as well as flatten them.
2) map() is used for transformation only, but flatMap() is used for both transformation and flattening.
class GFG
{
// Driver code
public static void main(String[] args)
{
// Creating a list of Prime Numbers
List<Integer> PrimeNumbers = Arrays.asList(5, 7, 11,13);