artist = "David Bowie"; switch (artist) { case "Michael Jackson": System.out.println("I'm a pop singer."); break; case "David Bowie": System.out.println("I'm a rock singer."); break; case "Frank Sinatra": System.out.println("I'm a jazz singer."); break; default: System.out.println("I'm not a great singer."); break; } }
FileSystems.getDefault().getPathMatcher("glob:**/*.txt"); //regex can also be used Path path = SOURCE_FILE.toPath(); if (matcher.matches(path)) { System.out.println(path); } } @Test public void test_copyFileJava7() throws IOException{ Files.copy(SOURCE_FILE.toPath(), TARGET_FILE.toPath()); Assert.assertTrue(TARGET_FILE.exists()); }
composés d’instructions qui changent ces états à l’exécution. List<Student> students = … List<Student> tenOverFifty = new ArrayList<Student>(); int count = 0; for (Student student : students) { if (student.getScore() > 50) { tenOverFifty.add(student); count++; if (count >= 10) break; } } return tenOverFifty;
à l’exécution. List<Student> students = … return students.stream() .filter(s -> s.getScore() > 50) .limit(10); Avantage : lisibilité, non lié à un état du système, ...
= students.stream(); Generators Random random = new Random(); Stream randomNumbers = Stream.generate(random::nextInt); From other streams Stream newStream = Stream.concat(stream, randomNumbers);
match a Predicate .map transform element using a Function .flatMap transform each element into 0+ elements by way of another Stream .peek perform some action on each element .distinct exclude duplicates (equals()) .sorted ordered elements (Comparator) .limit max numbers of elements .substream range by index of elements
abstract void run(); } Runnable r = () -> System.out.println("Hello World!"); Historically have used single method interfaces to represent functions: Runnable, Comparator, ActionListener
- an action to be performed on an object Function<T, R> - a function transforming a T to a R Supplier<T> - provide an instance of a T (such as a factory) UnaryOperator<T> - a function from T to T BinaryOperator<T> - a function from (T, T) to T
method of a particular object (instanceRef::methName) A super method of a particular object (super::methName) An instance method of an arbitrary object of a particular type (ClassName::methName) A class constructor reference (ClassName::new) An array constructor reference (TypeName[]::new)
on veut les utiliser : List<?> list = ...; list.forEach(...); // lamdba code N’existe pas sur java.util.list. Ajouter des méthodes à l’ interface casserait les implémentations existantes. = Default methods = Defender methods Rétrocompatible : on peut ajouter une méthode par défaut sans casser l’existant. Cela réduit aussi la qté de code.
foo() { System.out.println(“A foo”); } } public interface B { default void foo() { System.out.println(“B foo”); } } public class Duck implements A, B { } Et ça compile?
: Date, Time, DateTime, Timestamp, Timezone, Period, Duration, Instant • Clareté : méthodes définies clairement et uniformément (plus de différences entre java.util.Date et java.sql.Date) • Méthodes utilitaires : plus, minus, format, … • Extensible : ouvert à d’autres systèmes de calendriers • Gestion en standard de l’i18n
public Optional<Soundcard> getSoundcard() { ... } } public class Soundcard { private Optional<USB> usb; public Optional<USB> getUSB() { ... } } public class USB{ public String getVersion(){ ... } } String version = computer.flatMap(Computer::getSoundcard) .flatMap(Soundcard::getUSB) .map(USB::getVersion) .orElse("UNKNOWN");