Java 8 — Just a little “explanation” 2

In the first part of this sequence you saw a new method on our List, the forEach. But, until Java 7 this method didn’t existed… how did it happen?

We know that if this method would be declared in an interface, we could have a big problem! What?

Everyone who implemented the interface would need to implement it.
Chaos would be installed, work would be immense, the Java community would go insane.
An interface like List would destroy libraries used by most of us, such as Hibernate.

Would be welcome to “NoSuchMethodErrors” Hell

BUT … we get a new feature in Java 8!

Default Methods

With this feature we can add a method to an interface and ensure that all implementations have it implemented. 🙂

For example, the forEach method we use is declared inside java.lang.Iterable, which is the parent of Collection, in turn List’s parent.

If we look at the method in the interface we’ll find:

default void forEach(Consumer <? Super T> action) {
   Objects.requireNonNull (action);
   for (T t: this) {
      action.accept (t);
   }
}

Yes, I think you never expected that, method with code inside interfaces..

As ArrayList implements List, which is (indirect) daughter of Iterable,
The ArrayList has this method, whether it wants to or not. It is for this reason that we can do it:

books.forEach(b -> System.out.println(b.getName()));

This reading serves as an explanation for the first part, which used this new feature.

Bye bye, see you!

References:
https://docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html

Java 8 — Just a little “hands on” 1

**moved from medium.com

I’m looking around and seeing many people working with older versions of Java.. then i will write some articles to stimulate this upgrade, to take doubts or discussions.

Let’s start in my first Medium article with a common practice.. a loop with FOR EACH.

Below you will take the old foreach:

List<Book> books = Arrays.asList(book1, book2, book3);for(Book u : books){
   System.out.println(b.getName());
}

Arrays.asList is a easy way to create an immutable List.

Now we can do for eachs with the new method of collections, forEach:

books.forEach(…);

This method receive a type java.util.function.Consumer with a unique method accept. This is from the new package java.util.function.

Let’s create this consumer, before use the new forEach:

class Display implements Consumer<Book> {
    public void accept(Book b){
      System.out.println(b.getName());
    }
}

Then we can do it:

Display display = new Display();
usuarios.forEach(display);

But, for this we can use anonymous classes like a good practice to simple tasks like that:

Consumer<Book> display = new Consumer<Book>() {
   public void accept(Book b){
      System.out.println(b.getName());
   }
};books.forEach(display);

Now it’s better! But, the code is still bigger, let’s change one more time:

books.forEach(new Consumer<Book>() {
   public void accept(Book b){
     System.out.println(b.getName());
   }
});

Yeah! Now we have the same, but not so verbose..

Hello, LAMBDA!

Now let’s know the lambda, this is it the Java simple way to implements an interface with ONLY ONE method. (Remember what we talk about the java.util.function.Consumer) 🙂

Consumer<Book> display = (Book b) -> {System.out.println(b.getName());};

WTF?! yes.. this is the same forEach! 😀

(Book b) -> {System.out.println(b.getName());}; 
//it’s a Java 8 lambda.

The compiler knows how to infer the type, and if the block have only one instruction we can remove the { }.

Consumer<Book> display = b -> System.out.println(b.getName());

What? A foreach on 1 line? Better… we don’t need a temporary variable.

books.forEach(b -> System.out.println(b.getName()));

It’s this.

Easy? EASY and CLEAN.

I will continue more explanations about Java 8, if you have any idea to share, tell me.
Thank you.