Core Java

Why Now is the Perfect Time to Upgrade to Java 8

Interested to see how you can get the most out of the new Java 8 features with AppDynamics? Start a FREE trial now!

This past March, Oracle released their most anticipated version in almost decade, Java 8. The latest version had a growing buzz since it had been announced, and companies of all sizes were eager to upgrade.

Our partner, Typesafe conducted a Java 8 adoption survey of 2,800 developers and found 65% of companies had already committed to adopting within the first 24 months of the release date.

Typesafe’s survey corroborated InfoQ’s survey of developers who stated 61% were devoted to adopting Java 8. Their handy heatmap below displays how excited developers were to get started with Java 8 and utilize the new features such as lambda expressions, date and time, and the Nashon JavaScript engine. In my opinion, the lambda expressions are by far the most exciting new Java 8 feature.

java-8-heatmap

So, why are folks so excited for Java 8?

Lambda Expressions and Stream Processing

What are they?

Lambda expressions are arguably the most exciting and interesting new feature with the Java 8 release. Not only is the feature itself exciting for engineers, the implications will have resounding effects on flexibility and productivity.

A lambda expression is essentially an anonymous function which can be invoked as a named function normally would, or passed as an argument to higher-order functions. The introduction of lambdas opens up aspects of functional programming to the predominantly object-oriented programming environment, enabling your code to be more concise and flexible.

Why is it useful?

Consider the task of parsing Twitter data from a given user’s home stream. Specifically, we’ll be creating a map of word length to a list of words of the same length from a user’s home stream.

For instance:

twitter-post

Should yield:

{
  2=[so, an], 
  3=[are, for], 
  4=[wont, here, some, tips], 
  7=[extreme], 
  8=[programs, makeover], 
  9=[sometimes, uninstall], 
  11=[misbehaving, application]
}

And of course, for many tweets this data is aggregated.

Using traditional Java loop constructs, this could be solved as follows:

lambdas-code

Lets break down what’s happening step-by-step:

– Fetch the Twitter home timeline
– For each status

  • Extract text
  • Remove punctuation
  • Gather in one big list of words

– For each word

  • Filter HTTP links and empty words
  • Add word to mapping of length to list of words of same length

Now, lets consider the solution using stream processing and lambdas:

lambdas-stream

The lambda solution follows the same logic, and is significantly shorter. To boot, this solution can be very easily parallelized. Listed below is the next version performing the same processing in parallel below:

lambdas-parallel

Though a contrived example for purposes of illustration, the implications here are profound.

By adding lambda expressions, code can be developed faster, be clearer, and overall more flexible.

Flexible Code

As mentioned earlier, the implications of adding lambda expressions are huge. Flexible code is one of the biggest advantages of this feature. In today’s Agile and rapid-release engineering environment, it’s imperative for your code to be amenable to change. Java has finally begun to close the gap on other more nimble programming languages.

As another example, let’s consider an enhancement request for our Twitter processor. In abstract, we wish to procure a list of Twitter timeline statuses which are deemed “interesting”. Concretely, the retweet count is greater than 1 and the status text contains the word “awesome”. This rather straightforward to implement, as outlined below:

twitter-timeline

Now, at some later point in time, suppose product management decides to change what it means for a tweet to be interesting. Specifically, we’ll need to provide a user interface where a user can indicate based on an available set of criteria how a Tweet is deemed interesting.

This poses an interesting set of challenges. First, a user interface should provide some representation of an available set of filter criteria. More importantly, that representation should manifest in the Twitter processor as a formal set of filter criteria applied in code. One approach, is to parameterize the filter such that calling code specifies that criteria. This strategy is illustrated as follows:

twitter-interesting-timeline

This grants calling code the ability to specify arbitrary filter criteria, realized by the UI component.

By disambiguating how the timeline is filtered from what criteria is imposed, the code is now flexible enough to accept arbitrary filter criteria.

Full code details can be found at the following Github repository.

Summary

In short, lambda expressions in Java 8 enable development of clear, concise code while maximizing flexibility to remain responsive to future changes.

Engineers, and entire companies, work better when they can spend more time innovating on new products on features rather than focusing a majority of their time firefighting existing problems and squashing bugs. With AppDynamics Java 8 support, you’re finally able to gain some of time back, become more efficient, and starting innovating again.

After implementing AppDynamics throughout their Java environment, Giri Nathan, VP of Engineering at Priceline.com stated, “The AppDynamics APM solution increases our agility by letting us instrument any new code on the fly,” says Nathan. “We can monitor everything from servlets and Enterprise JavaBeans entry points to JDBC exit points, which gives us an end-to-end view of our transactions.”

Interested to see how you can get the most out of the new Java 8 features with AppDynamics? Start a FREE trial now!

Anthony Kilman

At AppDynamics, Anthony Kilman works on Java instrumentation, including core Java Agent features and support for monitoring various JVM frameworks.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button