Junior — Middle
What functional capabilities and improvements in Java have you been using since version 8 and above?
sobes.tech AI
Answer from AI
Since Java 8 and above, I actively use the following features and improvements:
- Lambda expressions — allow writing more concise and readable code, especially when working with collections.
- Stream API — for convenient processing of collections with filtering, transformation, and data aggregation capabilities.
- Optional — for safe handling of potential null values, reducing the number of NullPointerExceptions.
- Default methods in interfaces — enable adding new methods to interfaces without breaking existing implementations.
- New methods in standard classes — for example, the
java.timeclass introduced a new date-time API that is more convenient and safer than the oldDateandCalendarclasses.
Example of using Stream API and 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]