1
Блок Т
What did you miss in Java
from 9 to 13?
2
How I am?
SOKOVETS ARTEM
Head of AutomationQA @SberTech
#java, #kotlin #flutter
asokovets@gmail.com/@SokovetsArtem
3
Agenda
1. Enhancements
2. CodeOne Pitch
3. Java Quiz
4
Local Variable Type Inference => var (JDK 10)
New reserved word var for defining local variables,
instead of explicit type specification
var techTalk = "Java 9 to 13?"; //var => String
var chars = techTalk.toCharArray()//var=>char[]
var mike = new Person("Mike", 47);//var=>Person
var hash = mike.hashCode(); //var => int
possible if the concrete type for a local variable can be
determined by the compiler using the definition on the
right side of the assignment.
5
Local Variable Type Inference => var (JDK 10)
Especially useful in the context of generics spelling
abbreviations:
ArrayList<String> var names = new ArrayList<String>();
var names = new ArrayList<String>();
names.add("Tim"); names.add("Tom");
// var => ArrayList<String> names
// var => Map<String, Long>
Map<String, Long> var personage =
Map.of("Tim", 47L, "Tom", 12L, "Michael", 47L);
6
Local Variable Type Inference => var (JDK 10)
var is not suitable for Lambda-expressions
Thus no conversion into var is possible, but leads to the error
message >>
“Lambda expression needs an explicit target-type”.
To avoid this mistake, the following cast should be
inserted:
var isAdultVar =
(Predicate<Map.Entry<String, Long>>)
entry -> entry.getValue() >= 18;
7
Stream API in JDK9
takeWhile(...) dropWhile(...)
ofNullable(...)
takeWhile(Predicate<T>) – processes elements as long as the
condition is met
8
Take While Scenario
9
TakeWhile
takeWhile(Predicate<T>) – processes elements as long as the
condition is met
Stream<String> productQuality = Stream.of("Good",
"Good", "Good", "Bad", "Good");
productQuality.takeWhile(pd ->
pd.contains("Good")).forEach(out::println);
10
Stream API in JDK9
dropWhile(Predicate<T>) – skips/drops elements as long as
the condition is met
takeWhile(...) dropWhile(...)
ofNullable(...)
11
DropWhile
dropWhile(Predicate<T>) – skips/drops elements as long as
the condition is met
IntStream marks = IntStream.of(2,3,5,5);
marks.dropWhile(it -> it < 4)
.forEach(out::println);
>> 5
>> 5
12
Combination of the two methods for extracting data
Stream<String> words = Stream.of(
"ab", "BLA", "Xy", "<START>","WELCOME", "TO",
”SBERBANK", ”TECH TALKS","<END>", "x", "y", "z");
Stream<String> extracted = words
.dropWhile(str ->!str.startsWith("<START>"))
.skip(1)
.takeWhile(str -> !str.startsWith("<END>"));
>> WELCOME
>> TO
>> SBERBANK
>> TECH TALKS
13
Stream API in JDK9
ofNullable(T) - method returns a sequential Stream containing
a single element if this stream is non-null otherwise method
returns an empty Stream.
takeWhile(...) dropWhile(...)
ofNullable(...)
14
OfNullable
List<String> collection = Arrays.asList("A", "B", "C");
Map<String, Integer> map = new HashMap<>() {
{ put("A", 10); put("C", 30);}
};
private Stream<Integer> getStream() {
return collection.stream().flatMap(s -> {
Integer temp = map.get(s);
return temp != null ? Stream.of(temp) :
Stream.empty();
});
}
private Stream<Integer> getStreamWithOfNullable() {
return collection.stream().flatMap(s ->
Stream.ofNullable(map.get(s)));
}
JDK8
JDK10
15
Optional<T> JDK 9 (ifPresentOrElse)
16
Optional<T> JDK 9 (or)
17
Optional<T> JDK 9 (stream)
18
Why ifPresentOrElse(…) ?
19
Why ifPresentOrElse(…) ?
20
Why or(…) ?
21
Why or(…) ?
22
java.util.Optional<T> (JDK 11)
23
Collection Factory Methods
24
The Class LocalDate
25
The Class LocalDate
26
Enhancements in java.lang.String: lines()
27
Enhancements in java.lang.String: repeat()
28
Utility-class java.nio.file.Files
29
Switch Expressions: Retrospect
DayOfWeek day = DayOfWeek.FRIDAY;
int numOfLetters = -1;
switch (day)
{
case MONDAY:
case FRIDAY:
case SUNDAY:
numOfLetters = 6;
break;
case TUESDAY:
numOfLetters = 7;
break;
case THURSDAY:
case SATURDAY:
numOfLetters = 8;
break;
case WEDNESDAY:
numOfLetters = 9;
break;
}
30
Switch Expressions: Retrospect
DayOfWeek day = DayOfWeek.FRIDAY;
int numLetters = -1;
switch(day)
{
case MONDAY, FRIDAY, SUNDAY -> numLetters = 6;
case TUESDAY -> numLetters = 7;
case THURSDAY, SATURDAY -> numLetters = 8;
case WEDNESDAY -> numLetters = 9;
}
Mapping of weekdays to their length… elegantly with Java
12 / 13:
31
Switch Expressions: Retrospect
Mapping of weekdays to their length… elegantly with Java
12 / 13:
DayOfWeek day = DayOfWeek.FRIDAY;
int numLetters = switch (day)
{
case MONDAY, FRIDAY, SUNDAY -> 6;
case TUESDAY -> 7;
case THURSDAY, SATURDAY -> 8;
case WEDNESDAY -> 9;
};
Return
Value
32
Text Blocks (JDK 13)
33
What else?
• HTTP/2 API Intro
• Multi-Threading and the Class CompletableFuture<T>
• Java Module
34
CodeOne Pitch
• Kotlin is growing up
• RPA – new way in automation testing
• Use Method Handler instead of Reflection API
• Test Engine for flexible test framework in Junit 5
• Quantum computers are coming
35
Java Quiz #1
Which can fill in the blank?
interface TheInterface {
____ int method() { return 1; } }
A.public
B.protected
C.no modifier
D.private
E.None of the above
36
Java Quiz #2
What is the output?
var a = 1; var b = a;
String c = b; System.out.println(c);
A.a does not compile
B.b does not compile
C.c does not compile
D.1
E.None of the above
37
Takeaways
• Try to learn new features in Java -> Kotlin
• Share your knowledge to colleagues

What did you miss in Java from 9-13?

  • 1.
    1 Блок Т What didyou miss in Java from 9 to 13?
  • 2.
    2 How I am? SOKOVETSARTEM Head of AutomationQA @SberTech #java, #kotlin #flutter asokovets@gmail.com/@SokovetsArtem
  • 3.
  • 4.
    4 Local Variable TypeInference => var (JDK 10) New reserved word var for defining local variables, instead of explicit type specification var techTalk = "Java 9 to 13?"; //var => String var chars = techTalk.toCharArray()//var=>char[] var mike = new Person("Mike", 47);//var=>Person var hash = mike.hashCode(); //var => int possible if the concrete type for a local variable can be determined by the compiler using the definition on the right side of the assignment.
  • 5.
    5 Local Variable TypeInference => var (JDK 10) Especially useful in the context of generics spelling abbreviations: ArrayList<String> var names = new ArrayList<String>(); var names = new ArrayList<String>(); names.add("Tim"); names.add("Tom"); // var => ArrayList<String> names // var => Map<String, Long> Map<String, Long> var personage = Map.of("Tim", 47L, "Tom", 12L, "Michael", 47L);
  • 6.
    6 Local Variable TypeInference => var (JDK 10) var is not suitable for Lambda-expressions Thus no conversion into var is possible, but leads to the error message >> “Lambda expression needs an explicit target-type”. To avoid this mistake, the following cast should be inserted: var isAdultVar = (Predicate<Map.Entry<String, Long>>) entry -> entry.getValue() >= 18;
  • 7.
    7 Stream API inJDK9 takeWhile(...) dropWhile(...) ofNullable(...) takeWhile(Predicate<T>) – processes elements as long as the condition is met
  • 8.
  • 9.
    9 TakeWhile takeWhile(Predicate<T>) – processeselements as long as the condition is met Stream<String> productQuality = Stream.of("Good", "Good", "Good", "Bad", "Good"); productQuality.takeWhile(pd -> pd.contains("Good")).forEach(out::println);
  • 10.
    10 Stream API inJDK9 dropWhile(Predicate<T>) – skips/drops elements as long as the condition is met takeWhile(...) dropWhile(...) ofNullable(...)
  • 11.
    11 DropWhile dropWhile(Predicate<T>) – skips/dropselements as long as the condition is met IntStream marks = IntStream.of(2,3,5,5); marks.dropWhile(it -> it < 4) .forEach(out::println); >> 5 >> 5
  • 12.
    12 Combination of thetwo methods for extracting data Stream<String> words = Stream.of( "ab", "BLA", "Xy", "<START>","WELCOME", "TO", ”SBERBANK", ”TECH TALKS","<END>", "x", "y", "z"); Stream<String> extracted = words .dropWhile(str ->!str.startsWith("<START>")) .skip(1) .takeWhile(str -> !str.startsWith("<END>")); >> WELCOME >> TO >> SBERBANK >> TECH TALKS
  • 13.
    13 Stream API inJDK9 ofNullable(T) - method returns a sequential Stream containing a single element if this stream is non-null otherwise method returns an empty Stream. takeWhile(...) dropWhile(...) ofNullable(...)
  • 14.
    14 OfNullable List<String> collection =Arrays.asList("A", "B", "C"); Map<String, Integer> map = new HashMap<>() { { put("A", 10); put("C", 30);} }; private Stream<Integer> getStream() { return collection.stream().flatMap(s -> { Integer temp = map.get(s); return temp != null ? Stream.of(temp) : Stream.empty(); }); } private Stream<Integer> getStreamWithOfNullable() { return collection.stream().flatMap(s -> Stream.ofNullable(map.get(s))); } JDK8 JDK10
  • 15.
    15 Optional<T> JDK 9(ifPresentOrElse)
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
    29 Switch Expressions: Retrospect DayOfWeekday = DayOfWeek.FRIDAY; int numOfLetters = -1; switch (day) { case MONDAY: case FRIDAY: case SUNDAY: numOfLetters = 6; break; case TUESDAY: numOfLetters = 7; break; case THURSDAY: case SATURDAY: numOfLetters = 8; break; case WEDNESDAY: numOfLetters = 9; break; }
  • 30.
    30 Switch Expressions: Retrospect DayOfWeekday = DayOfWeek.FRIDAY; int numLetters = -1; switch(day) { case MONDAY, FRIDAY, SUNDAY -> numLetters = 6; case TUESDAY -> numLetters = 7; case THURSDAY, SATURDAY -> numLetters = 8; case WEDNESDAY -> numLetters = 9; } Mapping of weekdays to their length… elegantly with Java 12 / 13:
  • 31.
    31 Switch Expressions: Retrospect Mappingof weekdays to their length… elegantly with Java 12 / 13: DayOfWeek day = DayOfWeek.FRIDAY; int numLetters = switch (day) { case MONDAY, FRIDAY, SUNDAY -> 6; case TUESDAY -> 7; case THURSDAY, SATURDAY -> 8; case WEDNESDAY -> 9; }; Return Value
  • 32.
  • 33.
    33 What else? • HTTP/2API Intro • Multi-Threading and the Class CompletableFuture<T> • Java Module
  • 34.
    34 CodeOne Pitch • Kotlinis growing up • RPA – new way in automation testing • Use Method Handler instead of Reflection API • Test Engine for flexible test framework in Junit 5 • Quantum computers are coming
  • 35.
    35 Java Quiz #1 Whichcan fill in the blank? interface TheInterface { ____ int method() { return 1; } } A.public B.protected C.no modifier D.private E.None of the above
  • 36.
    36 Java Quiz #2 Whatis the output? var a = 1; var b = a; String c = b; System.out.println(c); A.a does not compile B.b does not compile C.c does not compile D.1 E.None of the above
  • 37.
    37 Takeaways • Try tolearn new features in Java -> Kotlin • Share your knowledge to colleagues