DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • How to Convert XLS to XLSX in Java
  • Recurrent Workflows With Cloud Native Dapr Jobs
  • Java Virtual Threads and Scaling
  • Java’s Next Act: Native Speed for a Cloud-Native World

Trending

  • Build an MCP Server Using Go to Connect AI Agents With Databases
  • Segmentation Violation and How Rust Helps Overcome It
  • Optimize Deployment Pipelines for Speed, Security and Seamless Automation
  • A Complete Guide to Modern AI Developer Tools
  1. DZone
  2. Coding
  3. Java
  4. An Overview Between Java 8 and Java 11

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.

By 
Otavio Santana user avatar
Otavio Santana
DZone Core CORE ·
Apr. 25, 21 · Tutorial
Likes (25)
Comment
Save
Tweet
Share
19.4K Views

Join the DZone community and get the full member experience.

Join For Free

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:

Function: Represents 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:

  • Intro to Stream 
  • Stream API
  • Introduction to the Java Date/Time API
  • Java 11 New Features
  • New Features in Java 9
  • New Features in Java 10
  • Sample Source
Java (programming language)

Opinions expressed by DZone contributors are their own.

Related

  • How to Convert XLS to XLSX in Java
  • Recurrent Workflows With Cloud Native Dapr Jobs
  • Java Virtual Threads and Scaling
  • Java’s Next Act: Native Speed for a Cloud-Native World

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!