▪
▪
▪
▪
▪
▪
▪
▪
▪
▪
▪
▪
▪
▪
▪
String s = "Hello world!";
▪
String s = "Hello world!";
IntStream stream = [Link](); // creates a Stream on
// the letters of s
▪
String s = "Hello world!";
IntStream stream = [Link](); // creates a Stream on
// the letters of s
[Link](letter -> (char)letter)
.map(Character::toUpperCase)
.forEach([Link]::print);
> HELLO WORLD!
▪
String s1 = "Hello";
String s2 = "world";
String s = s1 + " " + s2; // it works!
▪
String s1 = "Hello";
String s2 = "world";
String s = s1 + " " + s2; // it works!
▪
▪
StringBuffer sb1 = new StringBuffer();
[Link]("Hello");
▪
StringBuffer sb1 = new StringBuffer();
[Link]("Hello");
[Link](" ").append("world"); // can be chained
▪
StringBuffer sb1 = new StringBuffer();
[Link]("Hello");
[Link](" ").append("world"); // can be chained
String s = [Link]();
▪
StringBuffer sb1 = new StringBuffer();
[Link]("Hello");
[Link](" ").append("world"); // can be chained
String s = [Link]();
▪
▪
// The JDK 5 way
StringBuilder sb1 = new StringBuilder();
[Link]("Hello");
[Link](" ").append("world"); // can be chained
String s = [Link]();
▪
▪
// The JDK 5 way
StringBuilder sb1 = new StringBuilder();
[Link]("Hello");
[Link](" ").append("world"); // can be chained
String s = [Link]();
▪
▪
// The JDK 8 way
StringJoiner sj = new StringJoiner(", ");
[Link]("one").add("two").add("three");
▪
// The JDK 8 way
StringJoiner sj = new StringJoiner(", ");
[Link]("one").add("two").add("three");
String s = [Link]();
[Link](s);
> one, two, three
▪
// The JDK 8 way
StringJoiner sj = new StringJoiner(", ", "{", "}");
// we leave the joiner empty
String s = [Link]();
[Link](s);
▪
// The JDK 8 way
StringJoiner sj = new StringJoiner(", ", "{", "}");
// we leave the joiner empty
String s = [Link]();
[Link](s);
> {}
▪
// The JDK 8 way
StringJoiner sj = new StringJoiner(", ", "{", "}");
[Link]("one");
String s = [Link]();
[Link](s);
> {one}
▪
// The JDK 8 way
StringJoiner sj = new StringJoiner(", ", "{", "}");
[Link]("one").add("two").add("three");
String s = [Link]();
[Link](s);
> {one, two, three}
▪
// From the String class, with a vararg
String s = [Link](", ", "one", "two", "three");
[Link](s);
> one, two, three
▪
// From the String class, with an Iterable
String [] tab = {"one", "two", "three"};
String s = [Link](", ", tab);
[Link](s);
> one, two, three
▪
// Java 7 : try with resources
try (BufferedReader reader =
new BufferedReader(
new FileReader(
new File("d:/tmp/[Link]")));) {
Stream<String> stream = [Link]();
[Link](line -> [Link]("ERROR"))
.findFirst()
.ifPresent([Link]::println);
} catch (IOException ioe) {
// handle the exception
}
▪
// Java 7 : try with resources and use of Paths
Path path = [Link]("d:", "tmp", "[Link]");
try (Stream<String> stream = [Link](path)) {
[Link](line -> [Link]("ERROR"))
.findFirst()
.ifPresent([Link]::println);
} catch (IOException ioe) {
// handle the exception
}
▪
// Java 7 : try with resources and use of Paths
Path path = [Link]("d:", "tmp", "[Link]");
try (Stream<String> stream = [Link](path)) {
[Link](line -> [Link]("ERROR"))
.findFirst()
.ifPresent([Link]::println);
} catch (IOException ioe) {
// handle the exception
}
▪
▪
// Java 7 : try with resources and use of Paths
Path path = [Link]("c:", "windows");
try (Stream<Path> stream = [Link](path)) {
[Link](path -> [Link]().isDirectory())
.forEach([Link]::println);
} catch (IOException ioe) {
// handle the exception
}
▪
// Java 7 : try with resources and use of Paths
Path path = [Link]("c:", "windows");
try (Stream<Path> stream = [Link](path)) {
[Link](path -> [Link]().isDirectory())
.forEach([Link]::println);
} catch (IOException ioe) {
// handle the exception
}
▪
▪
// Java 7 : try with resources and use of Paths
Path path = [Link]("c:", "windows");
try (Stream<Path> stream = [Link](path)) {
[Link](path -> [Link]().isDirectory())
.forEach([Link]::println);
} catch (IOException ioe) {
// handle the exception
}
▪
// Java 7 : try with resources and use of Paths
Path path = [Link]("c:", "windows");
try (Stream<Path> stream = [Link](path, 2)) {
[Link](path -> [Link]().isDirectory())
.forEach([Link]::println);
} catch (IOException ioe) {
// handle the exception
}
▪
▪
▪
▪
// Unfortunately not for arrays
List<String> strings =
[Link]("one", "two", "three");
[Link]([Link]::println);
▪
// removes an element on a predicate
Collection<String> strings =
[Link]("one", "two", "three", "four");
// will not work if list is unmodifiable
Collection<String> list = new ArrayList<>(strings);
// returns true if the list has been modified
boolean b = [Link](s -> [Link]() > 4);
[Link](
[Link]().collect([Link](", ")));
▪
// removes an element on a predicate
Collection<String> strings =
[Link]("one", "two", "three", "four");
// will not work if list is unmodifiable
Collection<String> list = new ArrayList<>(strings);
// returns true if the list has been modified
boolean b = [Link](s -> [Link]() > 4);
[Link](
[Link]().collect([Link](", ")));
> one, two, four
▪
// removes an element on a predicate
List<String> strings =
[Link]("one", "two", "three", "four");
// will not work if list is unmodifiable
List<String> list = new ArrayList<>(strings);
// doesnt return anything
[Link](String::toUpperCase);
[Link](
[Link]().collect([Link](", ")));
▪
// removes an element on a predicate
List<String> strings =
[Link]("one", "two", "three", "four");
// will not work if list is unmodifiable
List<String> list = new ArrayList<>(strings);
// doesnt return anything
[Link](String::toUpperCase);
[Link](
[Link]().collect([Link](", ")));
> ONE, TWO, THREE, FOUR
▪
// removes an element on a predicate
List<String> strings =
[Link]("one", "two", "three", "four");
// will not work if list is unmodifiable
List<String> list = new ArrayList<>(strings);
// doesnt return anything
[Link]([Link]());
[Link](
[Link]().collect([Link](", ")));
▪
// removes an element on a predicate
List<String> strings =
[Link]("one", "two", "three", "four");
// will not work if list is unmodifiable
List<String> list = new ArrayList<>(strings);
// doesnt return anything
[Link]([Link]());
[Link](
[Link]().collect([Link](", ")));
> four, one, three, two
▪
// comparison using the last name
Comparator<Person> compareLastName =
new Comparator<Person>() {
@Override
public int compare(Person p1, Person p2) {
return [Link]().compareTo([Link]());
}
};
▪
// comparison using the last name
Comparator<Person> compareLastName =
new Comparator<Person>() {
@Override
public int compare(Person p1, Person p2) {
return [Link]().compareTo([Link]());
}
};
▪
▪
// comparison using the last name then the first name
Comparator<Person> compareLastNameThenFirstName =
new Comparator<Person>() {
@Override
public int compare(Person p1, Person p2) {
int lastNameComparison =
[Link]().compareTo([Link]());
return lastNameComparison == 0 ?
[Link]().compareTo([Link]());
lastNameComparison;
}
};
▪
▪
// comparison using the last name
Comparator<Person> compareLastName =
[Link](Person::getLastName);
▪
// comparison using the last name
Comparator<Person> compareLastName =
[Link](Person::getLastName);
▪
▪
// comparison using the last name and then the first name
Comparator<Person> compareLastNameThenFirstName =
[Link](Person::getLastName)
.thenComparing(Person::getFirstName);
▪
▪
// reverses a comparator
Comparator<Person> comp = ...;
Comparator<Person> reversedComp = [Link]();
▪
// compares comparable objects
Comparator<String> c = [Link]();
▪
// compares comparable objects
Comparator<String> c = [Link]();
// compares comparable objects in the reverse order
Comparator<String> c = [Link]();
▪
// considers null values lesser than non-null values
Comparator<String> c =
[Link]([Link]());
▪
// considers null values lesser than non-null values
Comparator<String> c =
[Link]([Link]());
// considers null values greater than non-null values
Comparator<String> c =
[Link]([Link]());
▪
▪
▪
long max = [Link](1L, 2L);
BinaryOperator<Long> sum = (l1, l2) -> l1 + l2;
= (l1, l2) -> [Link](l1, l2);
= Long::sum;
▪
// JDK 7
long l = 3141592653589793238L;
int hash = new Long(l).hashCode(); // -1985256439
▪
// JDK 7
long l = 3141592653589793238L;
int hash = new Long(l).hashCode(); // -1985256439
▪
▪
// JDK 7
long l = 3141592653589793238L;
int hash = new Long(l).hashCode(); // -1985256439
// JDK 8
long l = 3141592653589793238L;
int hash = [Link](l); // - 1985256439
▪
▪
Map<String, Person> map = ...;
[Link]((key, person) ->
[Link](key + " " + person);
▪
▪
Map<String, Person> map = ...;
Person p = [Link](key); // p can be null!
▪
Map<String, Person> map = ...;
Person defaultPerson = Person.DEFAULT_PERSON;
Person p = [Link](key, defaultPerson); // JDK 8
▪
▪
Map<String, Person> map = ...;
[Link](key, person); // will erase an existing person
▪
Map<String, Person> map = ...;
[Link](key, person);
[Link](key, person); // JDK8
▪
▪
Map<String, Person> map = ...;
[Link](key, person);
▪
▪
Map<String, Person> map = ...;
[Link](key, person);
[Link](key, oldPerson, newPerson);
▪
▪
Map<String, Person> map = ...;
[Link](key, person);
[Link](key, oldPerson, newPerson);
[Link]((key, oldPerson) -> newPerson);
▪
▪
Map<String, Person> map = ...;
[Link](key);
▪
Map<String, Person> map = ...;
[Link](key); // JDK 7
[Link](key, person); // JDK 8
▪
▪
Map<String, Person> map = ...;
[Link](key, person, (key, oldPerson) -> newPerson);
▪
▪
Map<String, Person> map = ...;
[Link](key, person, (key, oldPerson) -> newPerson);
▪
▪
Map<String, Person> map = ...;
[Link](key, key -> newPerson);
▪
▪
Map<String, Person> map = ...;
[Link](key, key -> newPerson);
Map<String, Map<Integer, Person>> bimap = ...;
Person p = ...;
[Link](key1, key -> new HashMap<>()).put(key2, p);
▪
Map<String, Person> map = ...;
[Link](key, person, (key, person) -> newPerson);
▪
▪
▪
@TestCases({
@TestCase(param=1, expected=false),
@TestCase(param=2, expected=true)
})
public boolean even(int param) {
return param % 2 == 0;
}
▪
▪
@TestCases({
@TestCase(param=1, expected=false),
@TestCase(param=2, expected=true)
})
public boolean even(int param) {
return param % 2 == 0;
}
▪
▪
▪
@TestCase(param=1, expected=false)
@TestCase(param=2, expected=true)
public boolean even(int param) {
return param % 2 == 0;
}
▪
▪
@TestCase(param=1, expected=false)
@TestCase(param=2, expected=true)
public boolean even(int param) {
return param % 2 == 0;
}
▪
▪
▪
▪
▪
▪
▪
@interface TestCase {
int param();
boolean expected();
}
@interface TestCases {
TestCase[] value();
}
▪
@Repeatable([Link])
@interface TestCase {
int param();
boolean expected();
}
@interface TestCases {
TestCase[] value();
}
▪
private @NonNull List<Person> persons = ... ;
▪
private @NonNull List<Person> persons = ... ;
private @NonNull List<@NonNull Person> persons = ... ;
▪