Show Menu
Cheatography

Java 8 Cheat Sheet (DRAFT) by

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Lambda

Optional type declar­ation
The compiler can infer the type
Optional parent­hesis around parameter
For single parameter, no need for parens. Required for multiple parameters
Optional curly braces
If it contains a single statement
Optional return keyword
Compiler automa­tically returns the value.
Facili­tates functional progra­mming.

Method references

Pass a method as a function parameter
public class Java8T­ester {

    public static void main(S­tring args[]){

      List names = new ArrayL­ist();

      names.a­dd­("Ge­org­e");

      names.a­dd­("No­wak­ows­ki");

      names.f­or­Eac­h(S­yst­em.o­ut­::p­rin­tln);

   }

}

Optional Class

Option­al.o­fN­ull­able()
Returns a non-empty Optional if a value present in the given object. Otherwise returns an empty Optiona
Option­al.e­mpty()
empty Optional object
Option­al.i­sP­res­ent()
Returns true if the given Optional object is non-empty
Option­a.i­fPr­esent()
Performs the given action if the Optional object is non-empty
Option­al.o­rE­lse()
Returns the value if present. Otherwise returns the given default value
Optional is a container object which is used to represent a value is present or absent.

Advant­ages:
1. Null checks are not required.
2. No more NPE at run-time
3. Encourages clean and neat APIs
4. No more Boiler plate code
 

Streams

Sequence of elements
A stream provides a set of elements of specific type in a sequential manner.
Source
Collec­tions, Arrays, or I/O resources
Aggregate operations
filter, map, limit, reduce, find, match
Pipelining
Most stream operations return a stream, so their result can be pipelined.
Automatic iterations
Stream operations do the iterations internally
stream()
Returns a sequential stream consid­ereing collection as its source
parall­elS­tream()
Returns a parallel stream
map
maps each element to its corres­ponding result
filter
used to elminiate elements based ona criteria.
limit
used to reduce the size of the stream
sorted
Collectors
Used to combine the result of processing on the elements of a stream. Can return a list or a string
Stream represents a sequence of objects from a source, which supports aggregate functions.
The concept of stream that lets the developer to process data declar­ati­vely, rather than having to write loops.
 

Method References

 
Help to point to methods by their names. A method reference is described using :: symbol. A method reference can be used to point to the following types of methods:

- Static methods
- Instance methods
- Constr­uctors using new operator (TreeS­et:­:new)

Nashorn JavaScript

 
Usage:

 Script­Engine engine = 

     new Script­Eng­ine­Man­age­r().ge­tEn­gin­eBy­Nam­e("n­ash­orn­");

        engine.ev­al(­"­pri­nt(­'Hello World! ');");


Or, you can read it in using a FileRe­ader() then call functions:

engine.ev­al(new FileRe­ade­r("t­est­Fun.js­")

Invocable invocable = (Invoc­able) engine;

Object result = 

invoca­ble.in­vok­eFu­nct­ion­("fu­n1", "­Peter Parker­");