Milliseid uusi võimalusi ja funktsioone Java 8-s kasutad oma arenduspraktikas?
sobes.tech AI
Vastus AI-lt
In Java 8, key features were introduced that significantly simplify and improve development:
-
Lambda expressions — allow writing compact and expressive code for functional interfaces, for example, for processing collections.
-
Stream API — a convenient way to work with data collections, supporting filtering, transformation, sorting, and aggregation operations in a declarative style.
-
Default methods in interfaces — enable adding new methods to interfaces without breaking existing implementations.
-
Optional — a class for safe handling of potentially null values, reducing the likelihood of NullPointerException.
-
New APIs for working with dates and times (java.time) — more convenient and reliable classes for handling dates, times, and time zones.
Example of using Stream API with lambdas:
List<String> names = Arrays.asList("Anna", "Bob", "Charlie", "David");
List<String> filtered = names.stream()
.filter(name -> name.startsWith("A"))
.map(String::toUpperCase)
.collect(Collectors.toList());
System.out.println(filtered); // [ANNA]