Java 8 New Stuff – Part I

Oracle released recently the newest version of Java, it is full of new and cool stuff like lambdas. However, some things are important improvements in JVM that could enhance the performance of some technologies such as Scala.

I will refer in this post two important things the new Interfaces, Streams and Optional.

  • Interfaces

The new Interfaces allow the existence of default methods, the concept is quite simple imagine that there is a method wich is used by all the classes wich implement the interface in old Java versions was necessary create a Abstract class and define there the method or create a static method in a Utils class.

But with Java 8 just create a default method that could be redefined in the implemented classes.

Note: If you are familiar with Scala the concept is similar to Traits, in fact this is quite helpful to the Scala compiler developers making a clean bytecode using the new Intefaces, instead of create abstract classes.

Code Example:

public interface Example {

   void execute();

   default String currentHourString(){

      ZonedDateTime dateTime = ZonedDateTime.now();
      return String.valueOf(dateTime.getHour());
   }

}

public class ExampleImpl implements Example {

   @Override
   public void execute() {

      //Implementation

   }

   @Override
   public String currentHourString() {
       return "Not Available";
   }
}

If you want to learn more about this just check the following article http://docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html .

  • Streams

Now is possible to make searches in collections, map-reduce operations easily.

This will unlock an array of possibilities wich could encrease the productivity of programmers,

In the future more frameworks will make implementations of Stream interface.

Code Example:


String europe = "Europe";

String asia = "Asia";

List<Pair<String,String>> countries = new ArrayList<>();

countries.add(new Pair(europe, "Warsaw"));

countries.add(new Pair(europe, "Lisbon"));

countries.add(new Pair(europe, "Madrid"));

countries.add(new Pair(europe, "London"));

countries.add(new Pair(asia, "Hong Kong"));

countries.add(new Pair(asia, "Singapore"));

Stream<Pair<String,String>> stream = countries.stream();

Iterator<Pair<String,String>> it = stream.filter((a) -> a.getKey().equalsIgnoreCase(europe)).iterator();

For more informations about this check http://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html .

Note: For .NET developers if you are familiar with LINQ, streams is similar, but SQL-like syntax is not available.

  • Optional

I will bet that as a Java Developer you are probably tired of Null Pointers Exceptions.

With Optional class is easier test if the objects are null, this is quite useful to avoid code repetition.

Using this it’s easy test if an object is not null and the field is also different from null.

This post is just an overview of the new features of Java 8 if you want something more deep please check the links avaible in this post.

Software Engineer. Startup culture enthusiast. I am taking a MSc in Computer Science with special focus in Software Engineering. "Think big, think further."

Your email address will not be published. Required fields are marked *