SlideShare une entreprise Scribd logo
@JosePaumard
nouvelles choses que l’on peut faire avec
Java
50 nouvelles choses que l'on peut faire avec Java 8
50 nouvelles choses que l'on peut faire avec Java 8
50 nouvelles choses que l'on peut faire avec Java 8
50 nouvelles choses que l'on peut faire avec Java 8
50 nouvelles choses que l'on peut faire avec Java 8
Questions ?
#50new8
@JosePaumard
Date
@JosePaumard#50new8
Date : Instant
Un instant est un point de la ligne du temps
Instant start = Instant.now() ;
Instant end = Instant.now() ;
@JosePaumard#50new8
Date : Duration
Une « duration » est une durée
Instant start = Instant.now() ;
Instant end = Instant.now() ;
Duration elapsed = Duration.between(start, end) ;
long millis = elapsed.toMillis() ;
@JosePaumard#50new8
Date : Duration
On peut faire des calculs sur les « durations »
Instant start = Instant.now() ;
Instant end = Instant.now() ;
Duration elapsed = Duration.between(start, end) ;
long millis = elapsed.toMillis() ;
elapsed.plus(2L, TemporalUnit.SECONDS) ;
@JosePaumard#50new8
Date : LocalDate
Une LocalDate est une date empirique
LocalDate now = LocalDate.now() ;
LocalDate shakespeareDoB =
LocaleDate.of(1564, Month.APRIL, 23) ;
@JosePaumard#50new8
Date : Period
Une Period est une durée entre LocalDate
LocalDate now = LocalDate.now() ;
LocalDate shakespeareDoB =
LocalDate.of(1564, Month.APRIL, 23) ;
Period p = shakespeareDoB.until(now) ;
System.out.println("# years = " + p.getYears()) ;
> # years = 449
@JosePaumard#50new8
Date : Period
Une Period est une durée entre LocalDate
LocalDate now = LocalDate.now() ;
LocalDate shakespeareDoB =
LocalDate.of(1564, Month.APRIL, 23) ;
Period p = shakespeareDoB.until(now) ;
System.out.println("# years = " + p.getYears()) ;
long days = shakespeareDoB.until(now, ChronoUnit.DAYS) ;
System.out.println("# days = " + days) ; // 164_354
@JosePaumard#50new8
Date : TemporalAdjuster
Permet de trouver une date à partir d’une autre
LocalDate now = LocalDate.now() ;
LocalDate nextSunday =
now.with(TemporalAdjuster.next(DayOfWeek.SUNDAY)) ;
@JosePaumard#50new8
Date : TemporalAdjuster
Permet de trouver une date à partir d’une autre
14 méthodes statiques dans la boite à outils
firstDayOfMonth(), lastDayOfYear()
firstDayOfNextMonth()
LocalDate now = LocalDate.now() ;
LocalDate nextSunday =
now.with(TemporalAdjuster.next(DayOfWeek.SUNDAY)) ;
@JosePaumard#50new8
Date : TemporalAdjuster
Permet de trouver une date à partir d’une autre
14 méthodes statiques dans la boite à outils
firstInMonth(DayOfWeek.MONDAY)
next(DayOfWeek.FRIDAY)
LocalDate now = LocalDate.now() ;
LocalDate nextSunday =
now.with(TemporalAdjuster.next(DayOfWeek.SUNDAY)) ;
@JosePaumard#50new8
Date : LocalTime
Permet de coder une heure empirique : 10h20
LocalTime now = LocalTime.now() ;
LocalTime time = LocalTime.of(10, 20) ; // 10h20
@JosePaumard#50new8
Date : LocalTime
Permet de coder une heure empirique : 10h20
LocalTime now = LocalTime.now() ;
LocalTime time = LocalTime.of(10, 20) ; // 10h20
LocalTime lunchTime = LocalTime.of(12, 30) ;
LocalTime coffeeTime = lunchTime.plusHours(2) ; // 14h20
@JosePaumard#50new8
Date : ZonedTime
Permet de coder des heures localisées
Set<String> allZonesIds = ZoneId.getAvailableZoneIds() ;
String ukTZ = ZoneId.of("Europe/London") ;
@JosePaumard#50new8
Date : ZonedTime
Permet de coder des heures localisées
System.out.println(
ZonedDateTime.of(
1564, Month.APRIL.getValue(), 23, // year / month / day
10, 0, 0, 0, // h / mn / s / nanos
ZoneId.of("Europe/London"))
); // prints 1564-04-23T10:00-00:01:15[Europe/London]
@JosePaumard#50new8
Date : ZonedTime
On peut faire des calculs sur les heures localisées
ZonedDateTime currentMeeting =
ZonedDateTime.of(
LocalDate.of(2014, Month.APRIL, 18), // LocalDate
LocalTime.of(9, 30), // LocalTime
ZoneId.of("Europe/London")
) ;
ZonedDateTime nextMeeting =
currentMeeting.plus(Period.ofMonth(1)) ;
@JosePaumard#50new8
Date : ZonedTime
On peut faire des calculs sur les heures localisées
ZonedDateTime currentMeeting =
ZonedDateTime.of(
LocalDate.of(2014, Month.APRIL, 18), // LocalDate
LocalTime.of(9, 30), // LocalTime
ZoneId.of("Europe/London")
) ;
ZonedDateTime nextMeeting =
currentMeeting.plus(Period.ofMonth(1)) ;
ZonedDateTime nextMeetingUS =
nextMeeting.withZoneSameInstant(ZoneId.of("US/Central")) ;
@JosePaumard#50new8
Date : Formattage
Classe utilitaire : DateTimeFormatter
ZonedDateTime nextMeetingUS =
nextMeeting.withZoneSameInstant(ZoneId.of("US/Central"));
System.out.println(
DateTimeFormatter.ISO_DATE_TIME.format(nextMeetingUS)
);
// prints 2014-04-12T03:30:00-05:00[US/Central]
System.out.println(
DateTimeFormatter.RFC_1123_DATE_TIME.format(nextMeetingUS)
);
// prints Sat, 12 Apr 2014 03:30:00 -0500
@JosePaumard#50new8
Date : liens avec java.util.Date
Classe utilitaire : DateTimeFormatter
Date date = Date.from(instant); // legacy -> new API
Instant instant = date.toInstant(); // API -> legacy
@JosePaumard#50new8
Date : liens avec java.util.Date
Classe utilitaire : DateTimeFormatter
Date date = Date.from(instant); // legacy -> new API
Instant instant = date.toInstant(); // API -> legacy
TimeStamp time = TimeStamp.from(instant); // legacy -> new API
Instant instant = time.toInstant(); // API -> legacy
@JosePaumard#50new8
Date : liens avec java.util.Date
Classe utilitaire : DateTimeFormatter
Date date = Date.from(instant); // legacy -> new API
Instant instant = date.toInstant(); // API -> legacy
TimeStamp time = TimeStamp.from(instant); // legacy -> new API
Instant instant = time.toInstant(); // API -> legacy
Date date = Date.from(localDate); // legacy -> new API
LocalDate localDate = date.toLocalDate(); // API -> legacy
@JosePaumard#50new8
Date : liens avec java.util.Date
Classe utilitaire : DateTimeFormatter
Date date = Date.from(instant); // legacy -> new API
Instant instant = date.toInstant(); // API -> legacy
TimeStamp time = TimeStamp.from(instant); // legacy -> new API
Instant instant = time.toInstant(); // API -> legacy
Date date = Date.from(localDate); // legacy -> new API
LocalDate localDate = date.toLocalDate(); // API -> legacy
Time time = Time.from(localTime); // legacy -> new API
LocalTime localTime = time.toLocalTime(); // API -> legacy
String
@JosePaumard#50new8
String : Stream
Un stream sur les lettres qui composent une String
String s = "bonjour" ;
IntStream stream = s.chars() ;
stream.forEach(Sytem.out::println) ;
@JosePaumard#50new8
String : Stream
Un stream sur les lettres qui composent une String
Affiche :
String s = "bonjour" ;
IntStream stream = s.chars() ;
stream
.map(String::toUpperCase)
.forEach(Sytem.out::print) ;
> BONJOUR
@JosePaumard#50new8
String : expressions régulières
Construction de Stream à partir d’une regexp
// book est une grrrande chaîne
Stream<String> words =
Pattern
.compile("[^p{javaLetter}]")
.splitAsStream(book) ;
@JosePaumard#50new8
String : concaténation
Le naïf écrit :
String s1 = "bonjour" ;
String s2 = "le monde" ;
String s3 = s1 + " " + s2 ;
@JosePaumard#50new8
String : concaténation
L’ignorant lui dit d’écrire :
StringBuilder sb1 = new StringBuilder("bonjour") ;
sb1.append(" le monde") ;
String s3 = sb1.toString() ;
@JosePaumard#50new8
String : concaténation
L’ignorant lui dit d’écrire :
String s1 = "bonjour" ;
String s2 = "le monde" ;
String s3 = s1 + " " + s2 ;LINENUMBER 10 L2
NEW java/lang/StringBuilder
DUP
ALOAD 1
INVOKESTATIC java/lang/String.valueOf(Ljava/lang/Object;)Ljava/lang/String;
INVOKESPECIAL java/lang/StringBuilder.<init>(Ljava/lang/String;)V
LDC " "
INVOKEVIRTUAL java/lang/StringBuilder.append(Ljava/lang/String;)Ljava/lang/StringBuilder;
ALOAD 2
INVOKEVIRTUAL java/lang/StringBuilder.append(Ljava/lang/String;)Ljava/lang/StringBuilder;
INVOKEVIRTUAL java/lang/StringBuilder.toString()Ljava/lang/String;
ASTORE 3
@JosePaumard#50new8
String : concaténation
Le spécialiste Java 8 écrit
// The JDK 8 way
StringJoiner sj = new StringJoiner(", ") ;
sj.add("one").add("two").add("three") ;
String s = sj.toString() ;
System.out.println(s) ;
@JosePaumard#50new8
String : concaténation
Le spécialiste Java 8 écrit
Ce qui affiche
// The JDK 8 way
StringJoiner sj = new StringJoiner(", ") ;
sj.add("one").add("two").add("three") ;
String s = sj.toString() ;
System.out.println(s) ;
> one, two, three
@JosePaumard#50new8
String : concaténation
Le spécialiste Java 8 écrit
Ce qui affiche
// The JDK 8 way
StringJoiner sj = new StringJoiner(", ", "{", "}") ;
sj.add("one").add("two").add("three") ;
String s = sj.toString() ;
System.out.println(s) ;
> {one, two, three}
@JosePaumard#50new8
String : concaténation
Le spécialiste Java 8 écrit
Ce qui affiche
// The JDK 8 way
StringJoiner sj = new StringJoiner(", ", "{", "}") ;
// on ne met rien dedans
String s = sj.toString() ;
System.out.println(s) ;
> {}
@JosePaumard#50new8
String : concaténation
S’utilise aussi à partir de String directement
Ce qui affiche
// From the String class, with a vararg
String s = String.join(", ", "one", "two", "three");
System.out.println(s);
> one, two, three
@JosePaumard#50new8
String : concaténation
S’utilise aussi à partir de String directement
Ce qui affiche
// From the String class, with an Iterable
String [] tab = {"one", "two", "three"} ;
String s = String.join(", ", tab) ;
System.out.println(s) ;
> one, two, three
I/O
@JosePaumard#50new8
I/O : lecture de fichiers texte
Stream implémente AutoCloseable
// Java 7 : try with resources and use of Paths
Path path = Paths.get("d:", "tmp", "debug.log");
try (Stream<String> stream = Files.lines(path)) {
stream.filter(line -> line.contains("ERROR"))
.findFirst()
.ifPresent(System.out::println);
} catch (IOException ioe) {
// handle the exception
}
@JosePaumard#50new8
I/O : lecture d’un répertoire
Files.list retourne les fichiers du répertoire
// Java 7 : try with resources and use of Paths
Path path = Paths.get("c:", "windows");
try (Stream<Path> stream = Files.list(path)) {
stream.filter(path -> path.toFile().isDirectory())
.forEach(System.out::println);
} catch (IOException ioe) {
// handle the exception
}
@JosePaumard#50new8
I/O : lecture d’une arborescence
Files.walk retourne les fichiers du sous-arbre
// Java 7 : try with resources and use of Paths
Path path = Paths.get("c:", "windows");
try (Stream<Path> stream = Files.walk(path)) {
stream.filter(path -> path.toFile().isDirectory())
.forEach(System.out::println);
} catch (IOException ioe) {
// handle the exception
}
@JosePaumard#50new8
I/O : lecture d’une arborescence
Files.walk retourne les fichiers du sous-arbre, profondeur
// Java 7 : try with resources and use of Paths
Path path = Paths.get("c:", "windows");
try (Stream<Path> stream = Files.walk(path, 2)) {
stream.filter(path -> path.toFile().isDirectory())
.forEach(System.out::println);
} catch (IOException ioe) {
// handle the exception
}
List
@JosePaumard#50new8
Iterable : forEach
ForEach : itère sur tous les éléments, prend un consumer
Ne marche pas sur les tableaux
// méthode forEach sur Iterable
List<String> strings =
Arrays.asList("one", "two", "three") ;
strings.forEach(System.out::println) ;
> one, two, three
@JosePaumard#50new8
Collection : removeIf
Retire un objet : prend un prédicat
// removes an element on a predicate
Collection<String> strings =
Arrays.asList("one", "two", "three", "four");
// works « in place », no Collections.unmodifiable...
Collection<String> list = new ArrayList<>(strings);
// returns true if the list has been modified
boolean b = list.removeIf(s -> s.length() > 4);
> one, two, four
@JosePaumard#50new8
List : replaceAll
Remplace un objet par sa transformée
// removes an element on a predicate
Collection<String> strings =
Arrays.asList("one", "two", "three", "four");
// works « in place », no Collections.unmodifiable...
Collection<String> list = new ArrayList<>(strings);
// returns nothing
list.replaceAll(String::toUpperCase);
> ONE, TWO, THREE, FOUR
@JosePaumard#50new8
List : sort
Tri une liste en place, prend un comparateur
// removes an element on a predicate
Collection<String> strings =
Arrays.asList("one", "two", "three", "four");
// works « in place », no Collections.unmodifiable...
Collection<String> list = new ArrayList<>(strings);
// returns nothing
list.sort(Comparator.naturalOrder()) ;
> four, one, three, two
Arrays
Parallel
@JosePaumard#50new8
Parallel Arrays
Arrays.parallelSetAll
long [] array = new long [...] ;
Arrays.parallelSetAll(array, index -> index % 3) ;
System.out.println(Arrays.toString(array)) ;
@JosePaumard#50new8
Parallel Arrays
Arrays.parallelPrefix : fold right
long [] array = new long [...] ;
Arrays.parallelPrefix(array, (l1, l2) -> l1 + l2) ;
System.out.println(Arrays.toString(array)) ;
long [] array = {1L, 1L, 1L, 1L} ;
> [1, 2, 3, 4]
@JosePaumard#50new8
Parallel Arrays
Arrays.sort : tri en place
long [] array = new long [...] ;
Arrays.parallelSort(array) ;
System.out.println(Arrays.toString(array)) ;
Comparator
@JosePaumard#50new8
Comparator !
Que dire de plus ?
Comparator.naturalOrder()
@JosePaumard#50new8
Comparator !
Que dire de plus ?
Comparator.naturalOrder()
public static
<T extends Comparable<? super T>> Comparator<T> naturalOrder() {
return (Comparator<T>)
Comparators.NaturalOrderComparator.INSTANCE;
}
@JosePaumard#50new8
Comparator !
enum NaturalOrderComparator
implements Comparator<Comparable<Object>> {
INSTANCE;
}
@JosePaumard#50new8
Comparator !
enum NaturalOrderComparator
implements Comparator<Comparable<Object>> {
INSTANCE;
public int compare(Comparable<Object> c1, Comparable<Object> c2) {
return c1.compareTo(c2);
}
}
@JosePaumard#50new8
Comparator !
enum NaturalOrderComparator
implements Comparator<Comparable<Object>> {
INSTANCE;
public int compare(Comparable<Object> c1, Comparable<Object> c2) {
return c1.compareTo(c2);
}
public Comparator<Comparable<Object>> reversed() {
return Comparator.reverseOrder();
}
}
@JosePaumard#50new8
Comparator !
Que dire de plus ?
Comparator.comparingBy(Person::getLastName)
.thenComparing(Person::getFirstName)
.thenComparing(Person::getAge)
Map
@JosePaumard#50new8
Map : forEach
Prend un BiConsumer
// the existing map
Map<String, Person> map = ... ;
map.forEach(
(key, value) -> System.out.println(key + " -> " + value)
) ;
@JosePaumard#50new8
Map : replace
Remplace une valeur avec sa clé
// the existing map
Map<String, Person> map = ... ;
// key, newValue
map.replace("six", john) ;
// key, oldValue, newValue
map.replace("six", peter, john) ;
@JosePaumard#50new8
Map : replaceAll
Transforme toutes les valeurs
// the existing map
Map<String, Person> map = ... ;
// key, oldValue
map.replaceAll(
(key, value) -> key + " -> " + value ;
) ;
@JosePaumard#50new8
Map : remove
Retire les paires clés / valeurs
// the existing map
Map<String, Person> map = ... ;
// key, oldValue
map.remove("six", john) ;
@JosePaumard#50new8
Map : compute
Calcule une valeur à partir de la clé et la valeur existante, et
d’une fonction qui fusionne la paire clé / valeur
// the existing map
Map<String, Person> map = ... ;
// key, oldValue
map.compute(
key,
(key, value) -> key + "::" + value
) ;
@JosePaumard#50new8
Map : merge
Calcule une valeur à partir de la clé, de l’actuelle valeur si
elle existe, et d’une fonction qui fusionne les valeurs
// the existing map
Map<String, Person> map = ... ;
// key, otherValue
map.merge(
key,
otherValue,
(value, otherValue) -> value.concat(", ").concat(otherValue)
) ;
@JosePaumard#50new8
Map : putIfAbsent
Ajoute une paire clé / valeur si la clé n’est pas déjà dans la
table
// the existing map
Map<String, Person> map = ... ;
// key, newValue
map.putIfAbsent("un", john) ;
@JosePaumard#50new8
Map : computeIfAbsent
Si la clé est absente : associe une valeur calculée par
exécution de la fonction. Dans tous les cas : retourne la
valeur (nouvelle ou ancienne)
// the existing map
Map<String, Map<String, Person>> map = ... ;
// key, newValue
map.computeIfAbsent(
"un",
key -> new HashMap<>()
)
.put(".un", john) ;
@JosePaumard#50new8
Map : computeIfPresent
Si la clé est présente : associe une valeur calculée par
exécution de la fonction. Retourne la valeur.
// the existing map
Map<String, Map<String, Person>> map = ... ;
// key, newValue
map.computeIfPresent(
"un", map,
(key, value) -> ... // la nouvelle valeur
)
.put(".un", john) ;
Completable
Future
@JosePaumard#50new8
CompletableFuture
Extension de Future
CompletableFuture<String> page =
CompletableFuture.supplyAsync(
) ;
@JosePaumard#50new8
CompletableFuture
Extension de Future
CompletableFuture<String> page =
CompletableFuture.supplyAsync(
() ->
readWebPage(url)
) ;
@JosePaumard#50new8
CompletableFuture
Permet de créer des pipelines
CompletableFuture.supplyAsync(
() ->
readWebPage(url)
)
.thenApply(content -> getImages(content)) ;
@JosePaumard#50new8
CompletableFuture
Permet de créer des pipelines
CompletableFuture<List<Image>> images =
CompletableFuture.supplyAsync(
() ->
readWebPage(url)
)
.thenApply(content -> getImages(content)) ; // function
@JosePaumard#50new8
CompletableFuture
thenCompose : composition de tâches dans le futur
CompletableFuture<List<Image>> cf =
CompletableFuture.supplyAsync(
() ->
readWebPage(url)
)
.thenCompose(content -> getImages(content))
@JosePaumard#50new8
CompletableFuture
thenCompose : composition de tâches dans le futur
CompletableFuture.supplyAsync(
() ->
readWebPage(url)
)
.thenCompose(content -> getImages(content))
.thenApply(image -> writeToDisk(image)) ; // retourne CF<Boolean>
@JosePaumard#50new8
CompletableFuture
thenCompose : composition de tâches dans le futur
List<CompletableFuture<Boolean>> result =
CompletableFuture.supplyAsync(
() ->
readWebPage(url)
)
.thenCompose(content -> getImages(content))
.thenApply(image -> writeToDisk(image)) ; // retourne CF<Boolean>
@JosePaumard#50new8
CompletableFuture
allOf : composition de tâches dans le futur (anyOf existe)
CompletableFuture.allOf(
CompletableFuture.supplyAsync(
() ->
readWebPage(url)
)
.thenCompose(content -> getImages(content))
.thenApply(image -> writeToDisk(image))
)
.join() ;
@JosePaumard#50new8
CompletableFuture
thenCombine : combine plusieurs CF
Applique la fonction une fois les deux CF exécutés
CompletableFuture cf1 = ... ;
CompletableFuture cf2 = ... ;
cf1.thenCombine(cf2, (b1, b2) -> b1 & b2) ; // retourne la combinaison
// des résultats des CF
@JosePaumard#50new8
CompletableFuture
thenCombine : combine plusieurs CF
Applique la fonction une fois les deux CF exécutés
thenAcceptBoth, runAfterBoth
CompletableFuture cf1 = ... ;
CompletableFuture cf2 = ... ;
cf1.thenCombine(cf2, (b1, b2) -> b1 & b2) ; // retourne la combinaison
// des résultats des CF
@JosePaumard#50new8
CompletableFuture
applyToEither : utilise le premier résultat disponible
CompletableFuture cf1 = ... ;
CompletableFuture cf2 = ... ;
cf1.applyToEither(cf2, (b) -> ...) ; // s’applique au résultat
// du premier CF dispo
@JosePaumard#50new8
CompletableFuture
applyToEither : utilise le premier résultat disponible
acceptEither, runAfterEither
CompletableFuture cf1 = ... ;
CompletableFuture cf2 = ... ;
cf1.applyToEither(cf2, (b) -> ...) ; // s’applique au résultat
// du premier CF dispo
Concurrence
@JosePaumard#50new8
Variables atomiques
On avait :
AtomicLong atomic = new AtomicLong() ;
long l1 = atomic.incrementAndGet() ;
@JosePaumard#50new8
Variables atomiques
On a :
AtomicLong atomic = new AtomicLong() ;
long l1 = atomic.incrementAndGet() ;
long l2 = atomic.updateAndGet(l -> l*2 + 1) ;
@JosePaumard#50new8
Variables atomiques
On a :
AtomicLong atomic = new AtomicLong() ;
long l1 = atomic.incrementAndGet() ;
long l2 = atomic.updateAndGet(l -> l*2 + 1) ;
long l3 = atomic.accumulateAndGet(12L, (l1, l2) -> l1 % l2) ;
@JosePaumard#50new8
LongAdder
On a :
LongAdded adder = new LongAdder() ;
adder.increment() ; // dans un thread
adder.increment() ; // dans un autre thread
adder.increment() ; // encore dans un autre thread
long sum = adder.sum() ;
@JosePaumard#50new8
LongAccumulator
Même chose, mais on généralise :
LongAccumulator accu =
new LongAccumulator((l1, l2) -> Long.max(l1, l2), 0L) ;
accu.accumulate(value1) ; // dans un thread
accu.accumulate(value2) ; // dans un autre thread
accu.accumulate(value2) ; // encore dans un autre thread
long sum = accu.longValue() ;
@JosePaumard#50new8
StampedLock
Un lock avec lecture optimiste
StampedLock sl= new StampedLock() ;
long stamp = sl.writeLock() ;
try {
...
} finally {
sl.unlockWrite(stamp) ;
}
long stamp = sl.readLock() ;
try {
...
} finally {
sl.unlockRead(stamp) ;
}
@JosePaumard#50new8
StampedLock
Un lock avec lecture optimiste
Exclusivité entre read / write, mais…
StampedLock sl= new StampedLock() ;
long stamp = sl.writeLock() ;
try {
...
} finally {
sl.unlockWrite(stamp) ;
}
long stamp = sl.readLock() ;
try {
...
} finally {
sl.unlockRead(stamp) ;
}
@JosePaumard#50new8
StampedLock
Un lock avec lecture optimiste
StampedLock sl= new StampedLock() ;
long stamp = sl.tryOptimisticRead() ;
// ici on lit une variable qui peut être modifiée par un autre thread
if (lock.validate(stamp)) {
// la lecture est validée
} else {
// un autre thread a acquis un write lock
}
ConcurrentConcurrent
HashMap
@JosePaumard#50new8
ConcurrentHashMap
Réécriture complète de ConcurrentHashMap V7
Complètement thread-safe
N’utilise de lock ≠ ConcurrentHashMap V7
Nouvelles méthodes
@JosePaumard#50new8
ConcurrentHashMap
6000 lignes de code
@JosePaumard#50new8
ConcurrentHashMap
6000 lignes de code
54 classes membre
@JosePaumard#50new8
ConcurrentHashMap
6000 lignes de code
54 classes membre
Pour info : 58 classes dans java.util.concurrent
@JosePaumard#50new8
ConcurrentHashMap
6000 lignes de code
54 classes membre
Pour info : 58 classes dans java.util.concurrent
Nouveaux patterns !
@JosePaumard#50new8
ConcurrentHashMap
Ne plus utiliser size()
int count = map.size() ; // ne pas utiliser
count = map.mappingCount() ; // nouvelle méthode
@JosePaumard#50new8
ConcurrentHashMap
Ne plus utiliser
int count = map.size() ; // ne pas utiliser
long count = map.mappingCount() ; // nouvelle méthode
@JosePaumard#50new8
ConcurrentHashMap
Recherche d’éléments
search(), searchKey(), searchValue(), searchEntry()
ConcurrentHashMap<Integer, String> map = ... ;
map.search(10, (key, value) -> value.length() < key) ;
@JosePaumard#50new8
ConcurrentHashMap
Recherche d’éléments
search(), searchKey(), searchValue(), searchEntry()
ConcurrentHashMap<Integer, String> map = ... ;
map.search(10, (key, value) -> value.length() < key) ;
@JosePaumard#50new8
ConcurrentHashMap
Recherche d’éléments
10 : taux de parallélisme
Si la table compte plus de 10 éléments, alors la recherche
se fait en parallèle !
ConcurrentHashMap<Integer, String> map = ... ;
map.search(10, (key, value) -> value.length() < key) ;
@JosePaumard#50new8
ConcurrentHashMap
Recherche d’éléments
10 : taux de parallélisme
Si la table compte plus de 10 éléments, alors la recherche
se fait en parallèle !
On peut passer 0, ou Integer.MAX_VALUE
ConcurrentHashMap<Integer, String> map = ... ;
map.search(10, (key, value) -> value.length() < key) ;
@JosePaumard#50new8
ConcurrentHashMap
ForEach
forEach(), forEachKey(), forEachEntries()
ConcurrentHashMap<Integer, String> map = ... ;
map.forEach(10,
(key, value) ->
System.out.println(String.join(key, "->", value)
) ;
@JosePaumard#50new8
ConcurrentHashMap
Réduction
reduce(), reduceKey(), reduceEntries()
ConcurrentHashMap<Integer, String> map = ... ;
map.reduce(10,
(key, value) -> value.getName(), // transformation
(name1, name2) -> name1.length() > name2.length() ?
name1 : name2) // reduction
) ;
@JosePaumard#50new8
Pas de ConcurrentHashSet
Mais…
Set<String> set = ConcurrentHashMap.<String>.newKeySet() ;
@JosePaumard#50new8
Pas de ConcurrentHashSet
Mais…
Crée une concurrent hashmap dont les valeurs sont
Boolean.TRUE
Sert de set concurrent
Set<String> set = ConcurrentHashMap.<String>.newKeySet() ;
50 nouvelles choses que l'on peut faire avec Java 8
50 nouvelles choses que l'on peut faire avec Java 8
@JosePaumard
#50new8

Contenu connexe

PDF
Java 8-streams-collectors-patterns
PDF
REST APIs with Spring
PDF
Deep Dive Java 17 Devoxx UK
PPTX
Spring Boot and REST API
PPTX
Spring Boot
PPTX
Workshop Spring - Session 1 - L'offre Spring et les bases
PDF
Spring Framework - Core
PPTX
Spring Framework
Java 8-streams-collectors-patterns
REST APIs with Spring
Deep Dive Java 17 Devoxx UK
Spring Boot and REST API
Spring Boot
Workshop Spring - Session 1 - L'offre Spring et les bases
Spring Framework - Core
Spring Framework

Tendances (20)

PDF
Real Life Clean Architecture
ODP
Xke spring boot
PPTX
Introduction à spring boot
KEY
Introdução ao Spring Framework
PPTX
Spring boot
PPTX
Introduction to Spring Boot
PDF
Microservice With Spring Boot and Spring Cloud
PPTX
Introduction to Spring Framework
PDF
Action Jackson! Effective JSON processing in Spring Boot Applications
PPTX
20180518 QNAP Seminar - Introduction to React Native
PDF
Spring boot introduction
PPTX
Introduction to Node js
PDF
Spring Framework - Spring Security
PPTX
Node js introduction
PDF
Spring Framework - AOP
PDF
Microservices with Java, Spring Boot and Spring Cloud
PPTX
Introduction to Maven
PPTX
Java Spring framework, Dependency Injection, DI, IoC, Inversion of Control
PPTX
Introduction to ansible
PPTX
MVC, MVVM, ReactorKit, VIPER를 거쳐 RIB 정착기
Real Life Clean Architecture
Xke spring boot
Introduction à spring boot
Introdução ao Spring Framework
Spring boot
Introduction to Spring Boot
Microservice With Spring Boot and Spring Cloud
Introduction to Spring Framework
Action Jackson! Effective JSON processing in Spring Boot Applications
20180518 QNAP Seminar - Introduction to React Native
Spring boot introduction
Introduction to Node js
Spring Framework - Spring Security
Node js introduction
Spring Framework - AOP
Microservices with Java, Spring Boot and Spring Cloud
Introduction to Maven
Java Spring framework, Dependency Injection, DI, IoC, Inversion of Control
Introduction to ansible
MVC, MVVM, ReactorKit, VIPER를 거쳐 RIB 정착기
Publicité

En vedette (20)

PDF
Java 8, Streams & Collectors, patterns, performances and parallelization
PDF
Autumn collection JavaOne 2014
PDF
50 new things you can do with java 8
PDF
Les Streams sont parmi nous
PDF
JDK 8, lambdas, streams, collectors - Bretagne Tour
PDF
API Asynchrones en Java 8
PDF
Better Product Definition with Lean UX and Design Thinking
PPTX
Introducing HTTP/2
PDF
Séminaire en ligne - Email Kinetic - 30 Mai 2017
PDF
Conference MicroServices101 - 1ere partie
PDF
Open Data v0.3
PPTX
Introduction to HTTP/2
PDF
Optimisez la performance de votre service client tout en maîtrisant votre b...
PDF
PDF
Going reactive in java
PDF
JAX-RS and CDI Bike the (Reactive) Bridge
PDF
Perf ug comment ne plus rajouter de ram a vos jvm sans savoir pourquoi
PDF
Microbox : Ma toolbox microservices - Julien Roy
PDF
NightClazz Java 8 Decouverte
PDF
So, you wanna migrate to Java 9?
Java 8, Streams & Collectors, patterns, performances and parallelization
Autumn collection JavaOne 2014
50 new things you can do with java 8
Les Streams sont parmi nous
JDK 8, lambdas, streams, collectors - Bretagne Tour
API Asynchrones en Java 8
Better Product Definition with Lean UX and Design Thinking
Introducing HTTP/2
Séminaire en ligne - Email Kinetic - 30 Mai 2017
Conference MicroServices101 - 1ere partie
Open Data v0.3
Introduction to HTTP/2
Optimisez la performance de votre service client tout en maîtrisant votre b...
Going reactive in java
JAX-RS and CDI Bike the (Reactive) Bridge
Perf ug comment ne plus rajouter de ram a vos jvm sans savoir pourquoi
Microbox : Ma toolbox microservices - Julien Roy
NightClazz Java 8 Decouverte
So, you wanna migrate to Java 9?
Publicité

Similaire à 50 nouvelles choses que l'on peut faire avec Java 8 (12)

PDF
50 nouvelles choses que l'on peut faire en Java 8
PDF
201303 - Java8
PDF
Android Optimisations Greendroid
PDF
Java 8 - DateTime
PDF
Geek Time Janvier 2017 : Java 8
PPTX
Marzouk collection-map
PPTX
JAVA8, créer votre future
PDF
JAVA Chapitre7
PPT
Ch2. Objets de base du JS.ppt semestre 1
PDF
Memo java
PDF
Memojava 100604104941-phpapp02
PDF
5- understandinggJava_Collections_v4.pdf
50 nouvelles choses que l'on peut faire en Java 8
201303 - Java8
Android Optimisations Greendroid
Java 8 - DateTime
Geek Time Janvier 2017 : Java 8
Marzouk collection-map
JAVA8, créer votre future
JAVA Chapitre7
Ch2. Objets de base du JS.ppt semestre 1
Memo java
Memojava 100604104941-phpapp02
5- understandinggJava_Collections_v4.pdf

Plus de José Paumard (20)

PDF
Loom Virtual Threads in the JDK 19
PDF
From Java 11 to 17 and beyond.pdf
PDF
The Future of Java: Records, Sealed Classes and Pattern Matching
PDF
Designing functional and fluent API: application to some GoF patterns
PDF
The Sincerest Form of Flattery
PPTX
The Sincerest Form of Flattery
PDF
Designing functional and fluent API: example of the Visitor Pattern
PDF
Construire son JDK en 10 étapes
PDF
Java Keeps Throttling Up!
PDF
Lambdas and Streams Master Class Part 2
PDF
Lambda and Stream Master class - part 1
PDF
Asynchronous Systems with Fn Flow
PDF
Java Full Throttle
PDF
Collectors in the Wild
PDF
Streams in the wild
PDF
JAX RS and CDI bike the reactive bridge
PDF
Free your lambdas
PDF
L'API Collector dans tous ses états
PDF
Linked to ArrayList: the full story
PDF
Free your lambdas
Loom Virtual Threads in the JDK 19
From Java 11 to 17 and beyond.pdf
The Future of Java: Records, Sealed Classes and Pattern Matching
Designing functional and fluent API: application to some GoF patterns
The Sincerest Form of Flattery
The Sincerest Form of Flattery
Designing functional and fluent API: example of the Visitor Pattern
Construire son JDK en 10 étapes
Java Keeps Throttling Up!
Lambdas and Streams Master Class Part 2
Lambda and Stream Master class - part 1
Asynchronous Systems with Fn Flow
Java Full Throttle
Collectors in the Wild
Streams in the wild
JAX RS and CDI bike the reactive bridge
Free your lambdas
L'API Collector dans tous ses états
Linked to ArrayList: the full story
Free your lambdas

Dernier (20)

PPTX
Saint Pierre Chrysologue, évêque de Ravenne «Docteur en homélies» (380-450).pptx
PPTX
Hopital bonne sante.pptxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
PDF
Sécurité des réseaux et data center .pdf
PDF
Mémorisation: guide à l'usage des professeurs en recherche de nouvelles idées
PPTX
VOCABULAIRE AU PRESCOLAIRE SENEGAL1.pptx
PDF
Créer sa vidéo pédagogique: interface nomade et principes pédas appliqués aux...
PDF
SEANCE_1_securite informatique des reserau.pdf
PPTX
Le tableau volé.pptx Film françaisde pascal Bonitzer
PPTX
LACTION-DIDACTIQUE-Les-theories-dapprentissage.pptx
PPTX
domaine 1.pptxhorgnv,,odl,vjfnghburjf,c,el
PDF
Referentiel des metiers cadres dans la banque
PPTX
L'évaluation-Pédagogique pour enseignants.pptx
PPTX
Les-Principales METHODES-PEDAGOGIQUES.pptx
PDF
Catalogue Formations et Conseil : INPED 2025 2026
PPT
CARIOLOGIE - Copie.ppt pour les étudiants
PPTX
3.1 COMPRENDRE LES NICHES sur les réseaux.pptx.pptx
PPTX
le-present-de-lindicatif-ou-le-subjonctif-present-exercice-grammatical-feuill...
PDF
Cours: Introduction à la Sécurité des Données
PPTX
Florence Delay.pptx Écrivaine française née à bayonne
PPTX
Marketing de l'Artisanat et la technique
Saint Pierre Chrysologue, évêque de Ravenne «Docteur en homélies» (380-450).pptx
Hopital bonne sante.pptxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Sécurité des réseaux et data center .pdf
Mémorisation: guide à l'usage des professeurs en recherche de nouvelles idées
VOCABULAIRE AU PRESCOLAIRE SENEGAL1.pptx
Créer sa vidéo pédagogique: interface nomade et principes pédas appliqués aux...
SEANCE_1_securite informatique des reserau.pdf
Le tableau volé.pptx Film françaisde pascal Bonitzer
LACTION-DIDACTIQUE-Les-theories-dapprentissage.pptx
domaine 1.pptxhorgnv,,odl,vjfnghburjf,c,el
Referentiel des metiers cadres dans la banque
L'évaluation-Pédagogique pour enseignants.pptx
Les-Principales METHODES-PEDAGOGIQUES.pptx
Catalogue Formations et Conseil : INPED 2025 2026
CARIOLOGIE - Copie.ppt pour les étudiants
3.1 COMPRENDRE LES NICHES sur les réseaux.pptx.pptx
le-present-de-lindicatif-ou-le-subjonctif-present-exercice-grammatical-feuill...
Cours: Introduction à la Sécurité des Données
Florence Delay.pptx Écrivaine française née à bayonne
Marketing de l'Artisanat et la technique

50 nouvelles choses que l'on peut faire avec Java 8