{{announcement.body}}
{{announcement.title}}

An Overview Between Java 8 and Java 11

DZone 's Guide to

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.

· Java Zone ·
Free Resource

One 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:

FunctionRepresents a function that accepts one argument and produces a result:

Java
 




x
14


 
1
import java.util.function.Function;
2
 
          
3
public class FunctionApp {
4
 
          
5
    public static void main(String[] args) {
6
        Function<String, Integer> toNumber = Integer::parseInt;
7
        System.out.println("To number: " + toNumber.apply("234"));
8
        Function<String, String> upperCase = String::toUpperCase;
9
        Function<String, String> trim = String::trim;
10
        Function<String, String> searchEngine = upperCase.andThen(trim);
11
        System.out.println("Search result: " + searchEngine.apply("   test one two   "));
12
 
          
13
    }
14
}



Predicate: Represents a predicate (boolean-valued function) of one argument.

Java
 




xxxxxxxxxx
1
10


1
import java.util.function.Predicate;
2
 
          
3
public class PredicateApp {
4
 
          
5
    public static void main(String[] args) {
6
        Predicate<String> startWithA = s -> s.startsWith("A");
7
        Predicate<String> startWithB = s -> s.startsWith("B");
8
        System.out.println(startWithA.and(startWithB).test("Animal"));
9
    }
10
}



Supplier: Represents a supplier of results.

Java
 




xxxxxxxxxx
1
11


1
import java.util.Optional;
2
import java.util.function.Supplier;
3
 
          
4
public class SupplierApp {
5
 
          
6
    public static void main(String[] args) {
7
        Supplier<String> cache = () -> "From Database";
8
        Optional<String> query = Optional.empty();
9
        System.out.println(query.orElseGet(cache));
10
    }
11
}



Consumer: Represents an operation that accepts a single input argument and returns no result.

Java
 




xxxxxxxxxx
1
10


1
import java.util.function.Consumer;
2
 
          
3
public class ConsumerApp {
4
 
          
5
    public static void main(String[] args) {
6
        Consumer<String> log = s -> System.out.println("The log " +s);
7
        Consumer<String> logB = s -> System.out.println("The logB " +s);
8
        log.andThen(logB).accept("The value A");
9
    }
10
}



On those interfaces, we'll see several improvements, such as in the Collections implementations:

Java
 




xxxxxxxxxx
1
11


 
1
public class ListApp {
2
 
          
3
    public static void main(String[] args) {
4
        List<String> fruits = new ArrayList<>(List.of("Bananas", "Melon", "Watermelon"));
5
        fruits.forEach(System.out::println);
6
        fruits.removeIf("Bananas"::equals);
7
        fruits.sort(Comparator.naturalOrder());
8
        System.out.println("After sort: ");
9
        fruits.forEach(System.out::println);
10
    }
11
}
12
 
          
13
public class SetApp {
14
 
          
15
    public static void main(String[] args) {
16
        Set<String> fruits = new HashSet<>(List.of("Bananas", "Melon", "Watermelon"));
17
        fruits.forEach(System.out::println);
18
        fruits.removeIf("Bananas"::equals);
19
        System.out.println("After sort: ");
20
        fruits.forEach(System.out::println);
21
    }
22
}
23
 
          
24
public class MapApp {
25
 
          
26
    public static void main(String[] args) {
27
        Map<String, String> medias = new HashMap<>();
28
        medias.put("facebook", "otaviojava");
29
        medias.put("twitter", "otaviojava");
30
        medias.put("linkedin", "otaviojava");
31
        System.out.println("The medias values " + medias);
32
        medias.forEach((k, v) -> System.out.println("the key: " + k + " the value " + v));
33
        medias.compute("twitter", (k, v) -> k + '-' + v);
34
        System.out.println("The medias values " + medias);
35
        medias.computeIfAbsent("social", k -> "no media found: " + k);
36
        medias.computeIfPresent("social", (k, v) -> k + " " + v);
37
        System.out.println("The medias values " + medias);
38
        medias.replaceAll((k, v) -> v.toUpperCase(Locale.ENGLISH));
39
        System.out.println("The medias values " + medias);
40
    }
41
}
42
 
          



Also, there are new methods factories to make it easier to create the collections interfaces: 

Java
 




xxxxxxxxxx
1
12


1
import java.util.List;
2
import java.util.Map;
3
import java.util.Set;
4
 
          
5
public class MethodFactory {
6
 
          
7
    public static void main(String[] args) {
8
        List<String> fruits = List.of("banana", "apples");
9
        Set<String> animals = Set.of("Lion", "Monkey");
10
        Map<String, String> contacts = Map.of("email", "me@gmail.com", "twitter", "otaviojava");
11
    }
12
}



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: 

Java
 




xxxxxxxxxx
1
12


1
public class StreamApp {
2
 
          
3
    public static void main(String[] args) {
4
        List<String> fruits = List.of("Banana", "Melon", "Watermelon");
5
        fruits.stream().sorted().collect(Collectors.collectingAndThen(Collectors.toList(), Collections::unmodifiableList));
6
        fruits.stream().sorted().collect(Collectors.toUnmodifiableList());
7
        Map<Boolean, List<String>> startWithB = fruits.stream().collect(Collectors.partitioningBy(f -> f.startsWith("B")));
8
        System.out.println("Start with B " + startWithB);
9
        Map<String, List<String>> initials = fruits.stream().collect(Collectors.groupingBy(s -> s.substring(0)));
10
        System.out.println("Initials: " + initials);
11
    }
12
}


Java
 




xxxxxxxxxx
1


1
 
          
2
public class StreamReduceApp {
3
 
          
4
    public static void main(String[] args) {
5
        List<BigDecimal> values = List.of(BigDecimal.ONE, BigDecimal.TEN);
6
        Optional<BigDecimal> total = values.stream().reduce(BigDecimal::add);
7
        System.out.println(total);
8
    }
9
}



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. 

 

Java
 




xxxxxxxxxx
1
20


1
import java.time.DayOfWeek;
2
import java.time.LocalDate;
3
import java.time.LocalDateTime;
4
import java.time.Month;
5
import java.time.Year;
6
import java.time.YearMonth;
7
import java.util.Arrays;
8
 
          
9
public class DateApp {
10
 
          
11
    public static void main(String[] args) {
12
        System.out.println("LocalDateTime: " + LocalDateTime.now());
13
        System.out.println("Localdate: " + LocalDate.now());
14
        System.out.println("LocalDateTime: " + LocalDateTime.now());
15
        System.out.println("YearMonth: " + YearMonth.now());
16
        System.out.println("Year: " + Year.now());
17
        System.out.println("Days of weeks: " + Arrays.toString(DayOfWeek.values()));
18
        System.out.println("Months: " + Arrays.toString(Month.values()));
19
    }
20
}



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:

Topics:
features, java, java 11, java 8, tutorial

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}