-
Notifications
You must be signed in to change notification settings - Fork 10
New Features in Java 8
HOME > [NFJS 2015](NFJS 2015) > [New Features in Java 8](New Features in Java 8)
Book: Java 8 Lambdas
Book: Java SE 8 for the Really Impatient
Ken Kousen @kenkousen - ken.kousen@kousenit.com https://github.com/kousen/java8
###Lambda Expressions Block of code with variables passed in
(String s1, String s2) ->
Integer.compare(s1.length(), s2.length())
Basically Lambdas are a way to have a Method be represented as an Object. They are like an anonymous inner class
There is no class Lambda. They are represented as Functional Interface
@FunctionalInterface
- tells you that this interface has only a single abstract method. In Java 8, Interfaces can now have some implementation (default methods, static methods…) - Not required, but it makes code much more readable.
You can only assign a Lambda to a Functional Interface.
There is a new Integer.compare because doing int j - int i is dangerous. In a lambda you could do (Integer i, Integer j) -> i - j; and you could get an integer overflow…so use Integer.compare to be safer.
###Method References
String::length
- This is a method reference
System.out::println
x -> System.out.println(x)
Math::max
(x,y) -> Math.max(x,y)
###Constructor References
MyClass::new
MyClass[]::new
java.util.function
- New package that contains target types for lambda and method references
For each Interface in java.util.function, you will see an Int, Double, Long version of it. Like Consumer, IntConsumer, DoubleConsumer, LongConsumer. This allows them to skip the auto-boxing, makes it faster. So use the Int/Double/Long versions if you know that is what you are dealing with.
###Default Methods The “default” keyword in an Interface allows you to add new methods to an Interface without breaking those who already implemented your interface. For example, with Comparator, they couldn’t add a new abstract method with out breaking thousands of applications. Now, they can add new methods with a default implementation and you can override it if you want to. This has allowed for some cool new feature in a bunch of the Interfaces we know about and use all the time. like:
- Comparator.reverse
- Comparator.thenComparing
- Iterable.forEach
###What if there is a conflict? Class vs Interface - Class always wins Interface vs Interface - Compiler error - but you can fix this by overriding the conflicting method in your class implementation
###Static Methods in an Interface see Comparator.comparing They used to do things like: We have the Collection Interface and then we have the Collections helper class with things like Collections.sort. Now you can have Collection.sort.
###Streams A sequence of elements Does not store the elements Does not change the source (Immutable) Operations are lazy when possible.
New default method in Collection::stream A Stream carries a Source through a Pipeline
Pretty much like Map/Reduce
There are default reductions: avg, max, min, sum, etc.
Easy to parallel. Replace .stream() with .parallelStream()
###Creating Streams Collection.stream() Stream.of(T values) Stream.generate(Supplier s) Stream.iterate(T seed, UnaryOperator f) - seed = initial value Stream.emptyStream()
###Transforming Streams filter(Predicate p) map(Function<T,R> mapper)
naming confusion…you can .map to a Map .map().toMap()
Collectors.summarizingDouble - gives you some interesting status on a stream
###Optionals Alternative to returning Object or null Optional value isPresent() get() ifPresent() orElse() - give you a default/fallback
How do things like Spring Data handle Optionals? Optional’s are not Serializable...
###Date's java.time - written by the Joda Time people Objects are immutable Instant Duration LocalDateTime ZonedDateTime
Instant - a point on the time line Instant.EPOCH Instant.MAX/MIN
Duration.between Duration.toNanos, toMillis, etc
LocalDate
- A date without time zone.
- LocalDate.of(2015, 1, 1) - months start at 1! Months are a enum now.
- local.atZone - convert to a timezone
ChronoUnit
###Concurrency updates
###Other Nashorn - javascript engine in Java 8 JavaFX
ACES Learn to Code
- Git, GitHub GH-Pages
- Ozone Platform Developer Setup
- HTML, JavaScript, CSS
- Tomcat Web Server Setup
- A Simple Node.js App
- Spark with Docker
- Best Practices for Software Development
Other Tutorials
Conferences
- 2018 - DevOps Days Baltimore
- 2018 DevOps Days Baltimore, Part 2
- DevOpsDays---Baltimore
- Cross-Domain-Technical-Forum
- 2017 Potential Conferences
- LAS December 5th 2016
- DI2E Plugfest 2016
- OSCON 2015
- RWX-2015
- SpringOne-2017
- OSCON-2018
- DinosaurJS 2018
Training
- Developing on AWS
- Agile Team Facilitation
- Amazon AWS Big Data Solutions Day
- Cloudera Developer Training for Spark and Hadoop May 2016