JDK 8 in examples

Recently, on an Oracle blog, Brian Goetz published a link to a presentation with Devoxx talk on Language / Library / VM Co-Evolution in Java SE 8.
In this article I will try to briefly reveal the main problems that will be solved in the eight, as well as ways to solve them that were selected by Oracle. Other aspects can be seen in the original.

Read on:
Extending existing backward compatible APIs;
Java is becoming even more object oriented;
Lambda expressions in Java;
Streamline multithreading.

So what Oracle wants to do:

I. Extending existing backward compatible APIs.


To simplify the life of all javists, it was decided to expand / supplement the JDK, as well as to enable everyone (including existing frameworks) to supplement their APIs. In this case, a prerequisite is the preservation of legacy-code. For this, the guys from Oracle made Virtual extension methods.
Virtual extension methods are methods that can be added to existing interfaces and provide a default implementation of these methods, while implementation classes will not require recompilation and will work as they did before. We get the preservation of the health of existing APIs on the one hand and the ability to expand functionality on the other.

An example is Iterator and UnsupportedOperationException.

We all know the remove method of the Iterator interface. The javadoc says that if the iterator implementation does not support the remove method, then it may throw an UnsupportedOperationException. Agree, crooked. If I'm not going to implement a method, then why should I define it and throw an exception?

Decision:

interfaceIterator<T> {
		  booleanhasNext();
		  T next();
		  voidremove()default{ thrownew UnsupportedOperationException(); };
		}
		

This defines the default implementation for the remove method. Implementation classes are no longer required to provide an implementation of this method.

An example is iterating over a list.

Suppose there is a code that determines the highest score among students in 2011:

		  List<Student> students = ...
		  double highestScore = 0.0;
		  for (Student s: students) {
		    if (s.getGradYear() == 2011) {
		      if (s.getScore() > highestScore) {
		        highestScore = s.score;
		      }
		    }
		  }
		

At first glance, a pretty decent code. But it is not object oriented. The iteration of the list itself takes place in the 'for' loop in the client code that uses this list. You can add virtual methods to collections and rewrite this code using the already object-oriented approach and the “internal” iteration.

		  SomeCoolList<Student> students = ...
		  double highestScore = 
		    students.filter(new Predicate<Student>() {
		      publicbooleanop(Student s){
		        return s.getGradYear() == 2011;
		      }
		    }).map(new Mapper<Student, Double>() {
		       public Double extract(Student s){
		         return s.getScore();
		       }
		    }).max();
		

Here already some iteration is performed by the SomeCoolList class. But you must admit, it is very cumbersome and verbose. And here lambda expression comes to the rescue.

Decision:


		  SomeCoolList<Student> students = ...
		  double highestScore = 
		    students.filter(Student s -> s.getGradYear() == 2011)
		            .map(Student s -> s.getScore())
		            .max();
		

This approach has another advantage - it is easier to parallelize the execution, since the collection itself passes through the collection.

II. Streamline Simplification


Since now computers with multi-core processors have already become the norm, it is obvious that it makes sense to use these very kernels, i.e. write multithreaded programs. Therefore, the next improvement in JDK 8 is simplification of concurrency.
The code above using only multithreading and virtual extension methods can be rewritten as follows:

		  SomeCoolList<Student> students = ...
		  double highestScore = 
		    students.parallel()
		            .filter(Student s -> s.getGradYear() == 2011)
		            .map(Student s -> s.getScore())
		            .max();
		


On this, the main major features of the Eight end.

I also remind you that you can download JDK Standard Edition 8 Developer Preview with Lambda Support here .

Also popular now: