An Overview Between Java 8 and Java 11
This tutorial covers the basics of Java 8 and Java 11; it is a start to prepare you for the next LTS: Java 17.
Join the DZone community and get the full member experience.
Join For FreeOne of the great news introduced in the Java world is related to the version cycle, of which we have a release every six months and a version of Long Term Support every three years. Currently, the LTS version is Java 11, from which many companies are moving towards its use. First, this movement is significant because new frameworks will not support Java 8 and will consider Java 11 as a minimum beyond the point that the next LTS will be in September 2021 with Java 11. The purpose of this article is to talk a little about the basic APIs that happens between Java 8 and Java 11.
The Functions Classes
An important point that will serve as a basis, starting with Java 8, is the new interfaces within the java.util.function
package, in this overview, we'll see covert four interfaces:
Function
: Represents a function that accepts one argument and produces a result:
import java.util.function.Function;
public class FunctionApp {
public static void main(String[] args) {
Function<String, Integer> toNumber = Integer::parseInt;
System.out.println("To number: " + toNumber.apply("234"));
Function<String, String> upperCase = String::toUpperCase;
Function<String, String> trim = String::trim;
Function<String, String> searchEngine = upperCase.andThen(trim);
System.out.println("Search result: " + searchEngine.apply(" test one two "));
}
}
Predicate
: Represents a predicate (boolean-valued function) of one argument.
xxxxxxxxxx
import java.util.function.Predicate;
public class PredicateApp {
public static void main(String[] args) {
Predicate<String> startWithA = s -> s.startsWith("A");
Predicate<String> startWithB = s -> s.startsWith("B");
System.out.println(startWithA.and(startWithB).test("Animal"));
}
}
Supplier
: Represents a supplier of results.
xxxxxxxxxx
import java.util.Optional;
import java.util.function.Supplier;
public class SupplierApp {
public static void main(String[] args) {
Supplier<String> cache = () -> "From Database";
Optional<String> query = Optional.empty();
System.out.println(query.orElseGet(cache));
}
}
Consumer
: Represents an operation that accepts a single input argument and returns no result.
xxxxxxxxxx
import java.util.function.Consumer;
public class ConsumerApp {
public static void main(String[] args) {
Consumer<String> log = s -> System.out.println("The log " +s);
Consumer<String> logB = s -> System.out.println("The logB " +s);
log.andThen(logB).accept("The value A");
}
}
On those interfaces, we'll see several improvements, such as in the Collections implementations:
xxxxxxxxxx
public class ListApp {
public static void main(String[] args) {
List<String> fruits = new ArrayList<>(List.of("Bananas", "Melon", "Watermelon"));
fruits.forEach(System.out::println);
fruits.removeIf("Bananas"::equals);
fruits.sort(Comparator.naturalOrder());
System.out.println("After sort: ");
fruits.forEach(System.out::println);
}
}
public class SetApp {
public static void main(String[] args) {
Set<String> fruits = new HashSet<>(List.of("Bananas", "Melon", "Watermelon"));
fruits.forEach(System.out::println);
fruits.removeIf("Bananas"::equals);
System.out.println("After sort: ");
fruits.forEach(System.out::println);
}
}
public class MapApp {
public static void main(String[] args) {
Map<String, String> medias = new HashMap<>();
medias.put("facebook", "otaviojava");
medias.put("twitter", "otaviojava");
medias.put("linkedin", "otaviojava");
System.out.println("The medias values " + medias);
medias.forEach((k, v) -> System.out.println("the key: " + k + " the value " + v));
medias.compute("twitter", (k, v) -> k + '-' + v);
System.out.println("The medias values " + medias);
medias.computeIfAbsent("social", k -> "no media found: " + k);
medias.computeIfPresent("social", (k, v) -> k + " " + v);
System.out.println("The medias values " + medias);
medias.replaceAll((k, v) -> v.toUpperCase(Locale.ENGLISH));
System.out.println("The medias values " + medias);
}
}
Also, there are new methods factories to make it easier to create the collections interfaces:
xxxxxxxxxx
import java.util.List;
import java.util.Map;
import java.util.Set;
public class MethodFactory {
public static void main(String[] args) {
List<String> fruits = List.of("banana", "apples");
Set<String> animals = Set.of("Lion", "Monkey");
Map<String, String> contacts = Map.of("email", "me@gmail.com", "twitter", "otaviojava");
}
}
Stream is a sequence of elements supporting sequential and parallel aggregate operations. We can think about a waterfall or a river flow. This API is robust and clean to manipulate a collection such as:
xxxxxxxxxx
public class StreamApp {
public static void main(String[] args) {
List<String> fruits = List.of("Banana", "Melon", "Watermelon");
fruits.stream().sorted().collect(Collectors.collectingAndThen(Collectors.toList(), Collections::unmodifiableList));
fruits.stream().sorted().collect(Collectors.toUnmodifiableList());
Map<Boolean, List<String>> startWithB = fruits.stream().collect(Collectors.partitioningBy(f -> f.startsWith("B")));
System.out.println("Start with B " + startWithB);
Map<String, List<String>> initials = fruits.stream().collect(Collectors.groupingBy(s -> s.substring(0)));
System.out.println("Initials: " + initials);
}
}
xxxxxxxxxx
public class StreamReduceApp {
public static void main(String[] args) {
List<BigDecimal> values = List.of(BigDecimal.ONE, BigDecimal.TEN);
Optional<BigDecimal> total = values.stream().reduce(BigDecimal::add);
System.out.println(total);
}
}
When Java 8 was released, a new Date and Time API with new types, immutable classes, practical news methods, and finally Enums to the day of weeks and months instead of numbers.
xxxxxxxxxx
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Month;
import java.time.Year;
import java.time.YearMonth;
import java.util.Arrays;
public class DateApp {
public static void main(String[] args) {
System.out.println("LocalDateTime: " + LocalDateTime.now());
System.out.println("Localdate: " + LocalDate.now());
System.out.println("LocalDateTime: " + LocalDateTime.now());
System.out.println("YearMonth: " + YearMonth.now());
System.out.println("Year: " + Year.now());
System.out.println("Days of weeks: " + Arrays.toString(DayOfWeek.values()));
System.out.println("Months: " + Arrays.toString(Month.values()));
}
}
As the current LTS, Java 11 has several features that make the developer live easier in their day job. This post was only an overview and quickly started with Java 11 and missed some Java 8. There are many references about the feature to each new version.
References:
Opinions expressed by DZone contributors are their own.
Comments