Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7839429
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T15:26:37+00:00 2026-06-02T15:26:37+00:00

What I want to do is very simple using good old loops. Say I

  • 0

What I want to do is very simple using good old loops.

Say I have an object A that contains a List of Bs.

public class A
{
  public List<B> myListOfB;
}

In some other method, I have a List of As. Basically, what I want to do is merge all the elements in the lists of Bs of all my As.

For instance, it’s very easy to write something like this using loops :

public List<B> someMethod(List<A> myListOfAs)
{
  List<B> toReturn = new ArrayList<A>();
  for(A myA : myListOfAs)
  {
    toReturn.addAll(myA.myListOfB);
  }
  return toReturn;
}

But I would like to do it in a more fonctionnal way using Guava. This example is very easy, but it could be much more complex with conditions for instance, hence it makes sense to use functional programming.

I’m very new to Guava. I’ve started using it for filtering and ordering, but I’m pretty sure it might also be possible to use it, but I haven’t been able to figure out how. I found this Combine multiple Collections into a single logical Collection? but it doesn’t really answer my question.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T15:26:38+00:00Added an answer on June 2, 2026 at 3:26 pm

    You can use a function to extract the list and concat to flatten the lists. This results in an Iterable.

    List<A> input;
    Function<A, List<B>> t = new Function<A, List<B>>() {
        @Override public List<B> apply(A input) { 
            return input.myListOfB; 
        }
    };
    Iterable<B> transform = Iterables.concat(Iterables.transform(input, t));
    

    You can create an List if you need it:

    ImmutableList<B> asList = ImmutableList.copyOf(transform);
    //or
    List<B> newArrayList = Lists.newArrayList(transform);
    

    Note: Normally, public fields of classes are static and immutable or private. Everything else will bring you in trouble.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Very simple question, but I want to start using a consistent naming convention for
Hello everyone my problem is very simple. Using entity framework code first I want
I have a very simple application test in which I want to drag and
Very simple Winforms application I want to deploy manually. Can all the referenced assemblies
I want to write a very simple script , which takes a process name
I want to build a very simple GUI based application in linux, c++. Which
I'll be very simple: I want to be able to store Java objects from
I'm creating a very simple django upload application but I want to make it
I just want to design this very simple website. Basically there are multiple pages
I want to use the Django admin interface for a very simple web application

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.