demodays / bjdd

BJDD 北京弟弟 - events in Beijing | 北京活动
http://demodays.github.io/bjdd/
1 stars 0 forks source link

Java 8 demo #1

Open paulvi opened 10 years ago

paulvi commented 10 years ago

Links:

paulvi commented 10 years ago

Java 8

Is not yet released. Eclipse support coming with Eclipse 4.4 Luna. Paul took a look at current status with JDT.

New features in Java 1.8

http://openjdk.java.net/projects/jdk8/features is confusing.

Selected are:

Lambda

Inner classes

btn.setOnAction(new EventHandler<ActionEvent>() {
    @Override
    public void handle(ActionEvent event) {
        System.out.println("Hello World!");
    }
}); 

with lambda becomes

btn.setOnAction(
    event -> System.out.println("Hello World!")
); 

Default methods

Interfaces are now closer to abstract classes:

public interface A {
  default void foo(){
     System.out.println("Calling A.foo()");
  }
}

public interface B {
  default void bar(){
     System.out.println("Calling B.bar()");
  }
}

public class Clazz implements A, B {
}   

In fact, this is inheritance from many parents.

Remove the Permanent Generation

That is definitely a big one. The Permanent Generation (PermGen) space has completely been removed and is kind of replaced by a new space called Metaspace.

The consequences of the PermGen removal is that obviously the PermSize and MaxPermSize JVM arguments are ignored and you will never get a java.lang.OutOfMemoryError: PermGen error.

Nashorn

Nashorn is new JavaScript engine

Small VM

Support the creation of a small VM that is no larger than 3MB.

JEP 148

Parallel Array Sorting

Java 8 introduces a new API for sorting: Arrays#parallelSort.

Arrays#parallelSort uses Fork/Join framework introduced in Java 7 to assign the sorting tasks to multiple threads available in the thread pool.

The different methods available for parallelSort are like parallelSort(byte[] a)

Bulk Data Operations for Collections

This is also called “filter/map/reduce for Java”. To bring this feature, lambda functions are used. There is both a serial and parallel implementation. For more information you can check out the bulk data operations for Collections page on java.net.

Let's see a code example of what you can do with this feature by doing some filtering on a List to find the ones matching a particular List which are names.

List<String> names = Arrays.asList("Smith", "Adams", "Crawford");
List<Person> people = peopleDAO.find("London");

// Using anyMatch and method reference
List<Person> allPeople = people.stream().filter(p -> (names.stream().anyMatch(p.name::contains))).collect(Collectors.toList());

// Using reduce
List<Person> reduced = people.stream().filter(p -> names.stream().reduce(false, (Boolean b, String keyword) -> b || p.name.contains(keyword), (l, r) -> l | r)).collect(Collectors.toList());

Given the peopleDAO.find("London") will return thousands of Person. With just one line, I can filter that big List to keep only the Person matching Smith, Adams and Crawford. That's pretty cool, isn't it?

New Date & Time API

Clock clock = Clock.systemUTC(); //return the current time based on your system clock and set to UTC.

Clock clock = Clock.systemDefaultZone(); //return time based on system clock zone

long time = clock.millis(); //time in milliseconds from January 1st, 1970 

ZoneId zone = ZoneId.systemDefault(); //get the ZoneId of the system
Clock clock = Clock.system(zone); //set the Clock to given zone

ZoneId zone = ZoneId.of("Europe/Berlin"); //get the ZoneId from timezone name

To deal with "human" date and time, you'll use LocalDate, LocalTime and LocalDateTime.

LocalDate date = LocalDate.now();
String year = date.getYear();
String month = date.getMonthValue();
String day = date.getDayOfMonth();

To do calculations with dates, it is also very easy. Probably the best improvement compared to the current situation with Java < 1.8

Period p = Period.of(2, HOURS);
LocalTime time = LocalTime.now();
LocalTime newTime = time.plus(p); // or time.plus(5, HOURS); or time.plusHours(5); 

Links