class: center, middle, title
layout: true .left-column[
- Underscores in Numeric Literals
- Strings in switch
- Diamond operator
- try-with-resources
- Catching Multiple Exception Types
.grey[
]]
name: 7_
public void java6()
{
long longNumber = 9876543210L;
long _5 = 5;
double pi = 3.1415926;
}
public void java7()
{
long longNumber = 9_876_543_210L;
long longNumberOddFormat = 987__65______43__210L;
long _5 = 5;
long invalidUnderscoreAtTheEnd = 98_;
double pi = 3.14_15_92_6;
double invalidPi = 3_.1415926;
double anotherInvalidPi = 3._1415926;
}
name: 7switch
public int dayInWeek_java6(String dayOfWeek) {
if(dayOfWeek.equalsIgnoreCase("Lundi")) return 1;
if(dayOfWeek.equalsIgnoreCase("Mardi")) return 2;
if(dayOfWeek.equalsIgnoreCase("Mercredi")) return 3;
if(dayOfWeek.equalsIgnoreCase("Jeudi")) return 4;
if(dayOfWeek.equalsIgnoreCase("Vendredi")) return 5;
if(dayOfWeek.equalsIgnoreCase("Samedi")) return 6;
if(dayOfWeek.equalsIgnoreCase("Dimanche")) return 7;
throw new IllegalArgumentException("Jour inconnu : " + dayOfWeek);
}
public int dayInWeek_java7(String dayOfWeek) {
* switch(dayOfWeek.toLowerCase()) {
case "lundi": return 1;
case "mardi": return 2;
case "mercredi": return 3;
case "jeudi": return 4;
case "vendredi": return 5;
case "samedi": return 6;
case "dimanche": return 7;
default: throw new IllegalArgumentException("Jour inconnu : " + dayOfWeek);
}
}
name: 7diamond
private Map<IK, Map<IK, List<Mouvement>>> regroupMvtByPalAndFract(List<Mouvement> movs) {
// MAP <IK palSV, <IK Fract, List<Mouvement>>>
Map<IK, Map<IK, List<Mouvement>>> mapPalFractMvt = new HashMap<IK, Map<IK,List<Mouvement>>>();
Map<IK, Map<IK, List<Mouvement>>> mapPalFractMvt = new HashMap<>();
private Palettes regroupMvtByPalAndFract(List<Mouvement> movs) {...}
public class Palettes {
private Map<IK, Fractions> fractions = new HashMap<>();
public Fractions getFractionsPalette(IK palette) {...}
}
public class Fractions {
private Map<IK, Mouvements> mouvements = new HashMap<>();
public Mouvements getMouvementsFraction(IK fraction) {...}
}
public class Mouvements {
private List<Mouvement> mouvements = new ArrayList<>();
}
name: 7try
public static int executeCount(Connection conn, String sqlQuery) throws SQLException {
Statement st = conn.createStatement();
try {
ResultSet resultSet = st.executeQuery(sqlQuery);
resultSet.next();
return resultSet.getInt(1);
} finally {
* st.close();
}
}
public static int executeCount(Connection conn, String sqlQuery) throws SQLException {
* try(Statement st = conn.createStatement())
{
ResultSet resultSet = st.executeQuery(sqlQuery);
resultSet.next();
return resultSet.getInt(1);
}
}
name: 7catch
public ISuiviVersion newInstance(Class<? extends ISuiviVersion versionClass) {
try {
return versionClass.newInstance();
} catch (InstantiationException e) {
throw new InfologicRuntimeException(/* TODO ERR */ null, e);
} catch (IllegalAccessException e) {
throw new InfologicRuntimeException(/* TODO ERR */ null, e);
}
}
public ISuiviVersion newInstance(Class<? extends ISuiviVersion versionClass) {
try {
return versionClass.newInstance();
} catch (InstantiationException | IllegalAccessException e) {
throw new InfologicRuntimeException(/* TODO ERR */ null, e);
}
}
layout: true .left-column[ .grey[
]
- Lambda Expressions
- Default Methods
- Method References
- Streams
- Date-Time Package
- Removal of PermGen
- Nashorn (js engine) ]
name: 8lambda
public void sortByLengthInJava7(String[] strings) {
Arrays.sort(strings, new Comparator<String> {
@Override
public int compare(String first, String second) {
* Integer.compare(first.length(), second.length());
}
});
}
public void sortByLengthInJava8(String[] strings) {
* Arrays.sort(strings, (first, second) -> Integer.compare(first.length(), second.length()));
}
public void sortByLengthInJava8(String[] strings) {
Arrays.sort(strings, (first, second) -> Integer.compare(first.length(), second.length()));
}
public class Arrays {
public static <T> void sort(T[] a, Comparator<? super T> c) {
...
}
...
}
*@FunctionalInterface
public interface Comparator<T> {
int compare(T o1, T o2);
...
}
???
- λ expressions can access effectively final variables from the enclosing scope.
name: 8default
@FunctionalInterface
public interface Comparator<T> {
public int compare(T o1, T o2);
public boolean equals(Object obj);
* public default Comparator<T> reversed() {
return Collections.reverseOrder(this);
}
* public default Comparator<T> thenComparing(Comparator<? super T> other) {
Objects.requireNonNull(other);
return (Comparator<T> & Serializable) (c1, c2) -> {
int res = compare(c1, c2);
return (res != 0) ? res : other.compare(c1, c2);
};
}
...
* public static <T extends Comparable<? super T>> Comparator<T> reverseOrder() {
return Collections.reverseOrder();
}
* public static <T extends Comparable<? super T>> Comparator<T> naturalOrder() {
return (Comparator<T>) Comparators.NaturalOrderComparator.INSTANCE;
}
...
}
???
- You can now add default and static methods to interfaces that provide concrete implementations.
- You must resolve any conflicts between default methods from multiple interfaces (
_8_Default
)
name: 8refs
public void sortByLengthInJava8(String[] strings) {
* Arrays.sort(strings, (first, second) -> Integer.compare(first.length(), second.length()));
}
public void sortByLengthInJava8(String[] strings) {
* Arrays.sort(strings, this::compareByLength);
}
private int compareByLength(String first, String second) {
return Integer.compare(first.length(), second.length());
}
public void sortIgnoringCaseInJava8(String[] strings) {
* Arrays.sort(strings, String::compareToIgnoreCase);
}
public final class String implements java.io.Serializable, Comparable<String>, CharSequence {
...
* public int compareToIgnoreCase(String str) {
return CASE_INSENSITIVE_ORDER.compare(this, str);
}
...
}
???
- Method and constructor references refer to methods or constructors without invoking them.
name: 8streams
public void printAll_java7(Collection<Integer> collection) {
for(Integer elt : collection) {
System.out.println(elt);
}
}
public void printAll_java8(Collection<Integer> collection) {
collection.stream().sorted().forEach(System.out::println);
}
... defined as sequences of steps (monads)
public void printAllLessThanTenOrdered(Collection<?> collection) {
collection.stream().filter(i -> i < 10).sorted().forEach(System.out::println);
}
public static IntStream range(int startInclusive, int endExclusive) {...}
public Stream<T> filter(Predicate<? super T> predicate);
public Optional<T> min(Comparator<? super T> comparator);
static public IK[] getIKs(IIdentifiable[] identifiables) {
IK[] res = new IK[identifiables.length];
for (int i = 0; i < identifiables.length; i++) {
res[i] = identifiables[i].getIk();
}
return res;
}
IK[] res = Arrays.stream(identifiables)
.map(IIdentifiable::getIk)
.toArray(n -> new IK[n]);
List<IK> res = Arrays.stream(identifiables)
.map(IIdentifiable::getIk)
.collect(Collectors.toList());
Map<IK, IIdentifiable> res = Arrays.stream(identifiables)
.collect(Collectors.toMap(IIdentifiable::getIk,
Function.identity()));
identifiables.parallelStream().map(IIdentifiable::getIk)
.collect(toList());
identifiables.parallelStream().map(IIdentifiable::getIk)
* .peek(ik -> Thread.sleep(999_999))
.collect(toList());
# Do not use *parallel streams* on server side --- layout: true --- class: center, middle, title # Questions ?