Show Menu
Cheatography

Java 8 Cheat Sheet (DRAFT) by

Lambdas, Stream API, Optional and Date/Time

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

Lambdas

java.u­til.fu­nction
Package
@Funct­ion­alI­nte­rface
Optional. Force a compiler error if the interface has more than one abstract method.
(String text) -> System.out.println(text)

(text) -> System.out.println(text)

System.out::println
Anonymous function.
Defaul­t/s­tatic method in interface

Stream API - Genera­toren

Iterate / Range
Automa­tische Erzeugung von IntStream durch Iteration
Builder
Aufbau eines Streams mit Elementen ohne Collection
Of
Sammlung ovn Objekten als Argument
Generate
Supplier Lambda zur Generi­erung von Einträgen
Files (NIO), ZipFile, JarFile, Patterns (RegEx)

Stream API - Transf­orm­ation

map( lambda )
Objekte von Eingabetyp in Ausgabetyp umwandeln
flatMap( lambda )
Collec­tio­n-E­lemente in Stream­-El­emente begradigen (Array[][] -> Array[])

Stream API - Combining / Shortening

.filter( p -> p.name.st­art­sWi­th(­"­M") )
.reduce( (x1, x2) -> x1+ x2)
y = ((x1 + x2) + x3) + ...
.colle­ct(­Col­lec­tor­s.j­oin­ing­(", "));
e1 + ", " + e2 + ...
.limit(5)
limit number of results
.skip(5)
skip elements

Stream API - Aggreg­ation

Those APIs consider the whole set
.sorted()
.min()­/.max()
.group­ingBy(
Gruppieren gleicher Elemente
.disti­nct()
make list unique based on lambda
.count()

Stream API - Ergebnisse

forEach / forEac­hOr­dered
toArra­y(S­tri­ng[­]::new)
collec­t(C­oll­ect­ors.to­Lis­t());
collec­t(C­oll­ect­ors.to­Col­lec­tio­n(A­rra­yLi­st:­:new));
 

Stream API

List<Person> persons1, persons2;
Stream.of(persons1, persons2)   // stream of lists
   .flatMap(ps -> ps.stream())      // stream of obj
   .map(p -> p.firstName + " " + p.name) // stream of name
   .forEach(System.out::println);

IntStream.iterate(0, i -> i + 1)
   .limit(10)
   .forEach(System.out::print);

Stream.builder()
   .add("Wir ").add("bauen ").add("einen Satz.").build()
   .forEachOrdered(...);

Stream.generate(() -> Double.valueOf(Math.random() * 100).intValue())
   .limit(5).forEach(...)

Java I/O, NIO

Files.lines(Paths.get(this.class.getResource("file.txt").toURI()))
   .count();

Date/Time API

LocalTime
LocalDate
LocalD­ateTime
ZonedD­ate­Tim­e.of() / .now(Z­one­Id.o­f(­"­CET­"));
Instant
Maschi­nen­zeit, genauer, ohne Zeitzone
Tempor­alA­djuster
Duration
DateTimeFormatter.ISO_OFFSET_DATE_TIME
// 2011-1­2-0­3T1­0:1­5:3­0+01:00
DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG)
// June 20, 2014; 10:15:58 GMT+1

Optional

Option­al<­Str­ing> result = doSomething();
if ( result.is­Pre­sent()) result.get();
String result = doSome­thi­ng(­).o­rEl­se(­"­unk­now­n");
doSome­thi­ng(­).i­fPr­ese­nt(­lam­bda);

Repeatable Annotation

@Repeatable(TableAnnos.class)
@Retention(RetentionPolicy.RUNTIME)
public @interface Table {
   String value();
}

@Retention(RetentionPolicy.RUNTIME)
public @interface TableAnnos {
   Table[] value();
}

@Table("Customer")
@Table("Employee")
public class Person {
}