Learn to Use New Java Features

O'Reilly Media
oreillymedia
Published in
3 min readOct 22, 2020

--

Editor’s Note: Keeping up with all the latest releases of Java can be overwhelming, but learning how to use new features can improve your skills as a programmer. Gail C. Anderson, Java Champion and experienced Java trainer, shares why it’s important to keep up with new Java features in her piece from 97 Things Every Java Programmer Should Know.

Java 8 introduced lambdas and streams, two game-changing features that give Java programmers significant language constructs. From Java 9 onward, release cycles occur every six months with more features popping up in each release. You should care about these new features because they help you write better code. And, your skills will improve as you incorporate new language paradigms into your programming arsenal.

Much has been written about streams and how they support a functional programming style, reduce bulky code, and make code more readable. So, let’s look at an example with streams, not so much to convince you to use streams everywhere but to entice you to learn about this and other Java features introduced since Java 8.

Our example computes the maximum, average, and minimum for systolic, diastolic, and pulse values from collected blood-pressure monitoring data. We want to visualize these computed summary statistics with a JavaFX bar chart.

Here’s a portion of our BPData model class, showing just the getter methods we need:

public class BPData {
...
public final Integer getSystolic() {
return systolic.get();
}
public final Integer getDiastolic() {
return diastolic.get();
}
public final Integer getPulse() {
return pulse.get();
}
...
}

The JavaFX bar chart creates the magic for this visualization. First, we need to build the correct series and feed our transformed data to the bar chart object. Since the operation is repeated for each series, it makes sense to create a single method to parameterize both the bar chart series and the specific BPData getter required to access this data. Our source data is stored in the variable sortedList, a date-sorted collection of BPData elements. Here’s the computeStatData method that builds our chart data:

private void computeStatData(
XYChart.Series<String, Number> targetList,
Function<BPData, Integer> f) {
// Set Maximum
targetList.getData().get(MAX).setYValue(sortedList.stream()
.mapToInt(f::apply)
.max()
.orElse(1));
// Set Average
targetList.getData().get(AVG).setYValue(sortedList.stream()
.mapToInt(f::apply)
.average()
.orElse(1.0));
// Set Minimum
targetList.getData().get(MIN).setYValue(sortedList.stream()
.mapToInt(f::apply)
.min()
.orElse(1));
}

Parameter targetList is the bar chart series data that corresponds to one of systolic, diastolic, or pulse data. We want to create a bar chart with the maximum, average, and minimum corresponding to each of these series. Thus, we set the chart’s Y-value to these computed values. The second parameter is the specific getter from BPData, passed as a method reference. We use this in the stream mapToInt method to access the specific values for that series. Each stream sequence returns the maximum, average, or minimum of the source data. Each terminating stream method returns orElse, an Optional object, making our bar chart display a placeholder value of 1 (or 1.0) if the source data stream is empty.

Here’s how to invoke this computeStatData method. The convenient method reference notation makes it easy to specify which BPData getter method to invoke for each data series:

computeStatData(systolicStats, 
BPData::getSystolic);
computeStatData(diastolicStats,
BPData::getDiastolic);
computeStatData(pulseStats, BPData::getPulse);

Prior to Java 8, this code was much more tedious to write. So, learning and using new Java features is a worthwhile skill to embrace as Java continues to improve.

For your next feature, how about checking out Java 14’s record syntax, a preview feature, to simplify the BPData class?

Learn faster. Dig deeper. See farther.

Join the O’Reilly online learning platform. Get a free trial today and find answers on the fly, or master something new and useful.

Learn more

Gail C. Anderson is a Java Champion, Oracle Groundbreaker Ambassador, and past member of the NetBeans Dream Team. She is director of research and founding member of the Anderson Software Group, a leading provider of training courses in Java, JavaFX, Python, Go, Modern C++, and other programming languages. Gail enjoys researching and writing about leading-edge Java technologies. Her current passion includes JavaFX with GraalVM for cross-platform mobile applications. She is the coauthor of eight textbooks on software programming. Most recently, she is a contributing author to The Definitive Guide to Modern Java Clients with JavaFX: Cross-Platform Mobile and Cloud Development (Apress). Gail has presented at various Java conferences and JUGS including Devoxx, Devnexus, JCrete, and Oracle Code/JavaOne worldwide. Twitter: @gail_asgteach. Website: asgteach.com.

--

--

O'Reilly Media
oreillymedia

O'Reilly Media spreads the knowledge of innovators through its books, video training, webcasts, events, and research.