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 8528271
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T08:45:52+00:00 2026-06-11T08:45:52+00:00

I just wonder what’s the best way to apply a function that returns Void

  • 0

I just wonder what’s the best way to apply a function that returns Void on an Iterable/Collection?

My usecase is:

  • i have a list of Animal objects
  • i want to call on each animal of the list the eat() function

I have a Function<Animal,Void> which calls input.eat();

It turns out that when i call:

Collections2.transform(animalList,eatFunction);

I dont find this very elegant since i’m not looking for a transformation, but only for an application without any output.
Worst, it does not even work since the Guava transformations are returning views.

What works fine is:

Lists.newArrayList( Collections2.transform(animalList,eatFunction) );

But it’s not elegant.
What is the best way to apply a Void function to an Iterable/Collection, in a functional way with Guava?

Edit:

  • Related question: Inverse of Supplier<T> in Guava
  • Also as Dzik points out, if you are using Java8 instead of Guava there’s the Consumer class: https://stackoverflow.com/a/32222080/82609
  • 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-11T08:45:53+00:00Added an answer on June 11, 2026 at 8:45 am

    What do you think is more elegant? A plain old for loop:

    for (Animal animal : animalList)
        animal.eat();
    

    or a “bending a procedural language by writing a procedural operation in a functional style” madness?

    static final Function<Animal, Void> eatFunction = new Function<Animal, Void>() {
        @Override
        public Void apply(Animal animal) {
            animal.eat();
            return null; // ugly as hell, but necessary to compile
        }
    }
    
    Lists.newArrayList(Collections2.transform(animalList, eatFunction));
    

    I would vote for the first case.

    If you really would like to write your programs in functional style, I would recommend switching to another JVM language.

    Scala might be a good alternative for such a case:

    animalList.foreach(animal => animal.eat)
    

    or even a shorter variant using the _ placeholder:

    animalList.foreach(_.eat)
    

    EDIT:

    After trying the code in Eclipse I found out that I had to add the return null statement to the eatFunction, because 1) Void isn’t the same as void and 2) it is uninstantiable. That’s even more ugly then expected! 🙂

    Also from performance point of view, the invocation of the lazy function by using some copy constructor like above also pointlessly allocates memory. An ArrayList of the same size as the animalList filled only with nulls is created just to be immediately garbage collected.

    If you really have a use case where you want to pass around some function objects and dynamically apply them on some collections, I would write my own functional interface and a foreach method:

    public interface Block<T> {
        void apply(T input);
    }
    
    public class FunctionUtils {
        public static <T> void forEach(Iterable<? extends T> iterable,
                Block<? super T> block) {
            for (T element : iterable) {
                block.apply(element);
            }
        }
    
    }
    

    Then you can similarly define a void (lower case) function:

    static final Block<Animal> eatFunction = new Block<Animal>() {
        @Override
        public void apply(Animal animal) {
            animal.eat();
        }
    };
    

    And use it like this:

    FunctionUtils.forEach(animalList, eatFunction);
    // or with a static import:
    forEach(animalList, eatFunction);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this question a while back. Just wonder what is the best way
I have method that calculates pi. I was just wonder if I could make
I just wonder what the best approach is to have multiple users work on
I just wonder there a way to display the files in document directory with
I just wonder if that is possible. I know simple types can be read
I just wonder if there is some convenient way to detect if overflow happens
I am a beginner to Haskell. I just wonder how to implement a function
I just wonder if that's possible to use Xcode's version control to connect to
I just wonder if there's an easy application that would allow me and others
This is the first time that I use transactions and I just wonder am

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.