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

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

SBOMs are essential to circumventing software supply chain attacks, and they provide visibility into various software components.

Related

  • Singleton: 6 Ways To Write and Use in Java Programming
  • Google Guava and Its Two Fantastic Libraries: Graph and Eventbus

Trending

  • Run Scalable Python Workloads With Modal
  • Rust: The Must-Adopt Language for Modern Software Development
  • Why Tailwind CSS Can Be Used Instead of Bootstrap CSS
  • How to Format Articles for DZone
  1. DZone
  2. Coding
  3. Java
  4. FlatMap in Guava

FlatMap in Guava

By 
Bill Bejeck user avatar
Bill Bejeck
·
May. 26, 15 · Interview
Likes (1)
Comment
Save
Tweet
Share
9.7K Views

Join the DZone community and get the full member experience.

Join For Free

This is a short post about a method I recently discovered in Guava.

The Issue

I had a situation at work where I was working with objects structured something like this:

public class Outer {
    String outerId;
    List<Inner> innerList;
    .......
}

public class Inner {
    String innerId;
    Date timestamp;
}

public class Merged {
    String outerId;
    String innerId;
    Date timestamp;
}

My task was flatten a list Outer objects (along with the list of Inner objects) into a list of Merged objects. Since I’m working with Java 7, using streams is not an option.

The First Solution

Instead I turn to the FluentIterable class from Guava. My first instinct is to go with the FluentIterable.transform method (which is essentially a map function):

List<Outer> originalList = getListOfObjects();

Function<Outer,List<Merged>> flattenFunction //Details left out for clarity

//returns an Iterable of Lists!
Iterable<List<Merged>> mergedObjects = FluentIterable.from(originalList).tranform(flattenFunction);

But I really want a single collection of Merged objects, not an iterable of lists! The missing ingredient here is a flatMap function. Since I’m not using Scala, Clojure or Java 8, I feel that I’m out of luck.

A Better Solution

I decide to take a closer look at the FluentIterable class and I discover the FluentIterable.transformAndConcat method. The transformAndConcat method applies a function to each element of the fluent iterable and appends the results into a single iterable instance. I have my flatMap function in Guava! Now my solution looks like this:

List<Outer> originalList = getListOfObjects();

Function<Outer,List<Merged>> flattenFunction //Details left out for clarity

Iterable<Merged> mergedObjects = FluentIterable.from(originalList).transformAndConcat(flattenFunction);

Conclusion

While this is a very short post, it goes to show how useful the Guava library is and how functional programming concepts can make our code more concise.



Google Guava

Published at DZone with permission of Bill Bejeck, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Singleton: 6 Ways To Write and Use in Java Programming
  • Google Guava and Its Two Fantastic Libraries: Graph and Eventbus

Partner Resources

×

Comments

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
  • [email protected]

Let's be friends: