Product::getInfo can be assigned to a Function<Product,Info> (since it's a non-static method, so it can be seen as a function that takes a Product instance and returns an Info instance). Change the declaration of functions from

List<Function> functions = new ArrayList<>();

to

List<Function<Product,Info>> functions = new ArrayList<>();

EDIT: I tested your code, and I get a different compilation error:

The type Product does not define getInfo(Object) that is applicable here.

The compilation error you got is misleading. Once I make the suggested change, the error goes away.

Answer from Eran on Stack Overflow
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › List.html
List (Java Platform SE 8 )
3 weeks ago - The List interface provides four methods for positional (indexed) access to list elements. Lists (like Java arrays) are zero based. Note that these operations may execute in time proportional to the index value for some implementations (the LinkedList class, for example).
🌐
GeeksforGeeks
geeksforgeeks.org › java › list-interface-java-examples
List Interface in Java - GeeksforGeeks
Since List is indexed, the element is replaced at the specified position. ... import java.util.*; class Geeks { public static void main(String args[]) { // Creating an object of List interface List<String> al = new ArrayList<>(); // Adding elements to object of List class al.add("Geeks"); al.add("Geeks"); al.add(1, "Geeks"); System.out.println("Initial ArrayList " + al); // Setting (updating) element at 1st index using set() method al.set(1, "For"); System.out.println("Updated ArrayList " + al); } }
Published   February 3, 2026
Discussions

Working with an ArrayList of Functions in Java-8 - Stack Overflow
Attempt to solve the problem: The ... many attempts to use the Function list. Again, please don't mind the design of the code. It was just done that way to try to make the problem example as short as possible. ... package com.Testing; import java.util.ArrayList; import ... More on stackoverflow.com
🌐 stackoverflow.com
testing - Creating a list of functions in java - Software Engineering Stack Exchange
I have a list of functions which need to be tested against a list of inputs to measure their relative performance. I have already create a test function like below: public static String testFunction( More on softwareengineering.stackexchange.com
🌐 softwareengineering.stackexchange.com
February 8, 2019
How do I create a List of functions in Java? - Stack Overflow
I intend to stream the functions and pass them the same data. ... Yes, naturally. ... The problem is that there's not enough type information to infer the parameterization of List, so it defaults to List. Either replace var with the actual type (e.g., List>) or use ... More on stackoverflow.com
🌐 stackoverflow.com
How to apply a list of functions to a value in Java 8? - Stack Overflow
Let's say I have this imperative code: List function : functions) { value = function.apply(v... More on stackoverflow.com
🌐 stackoverflow.com
Top answer
1 of 2
10

The method of a java.util.function.Function object is apply. You need to call it like this:

operations.get(i).apply(initialValue)

However you use raw Function and therefore the result could be Object and you'd need to convert it to the appropriate type. Also you can't use the + (or the +=) operator with it. I'd suggest restricting the parameter types with Number:

List<Function<Number, ? extends Number>> operations = Arrays.asList(
        num ->  num.intValue() + 1,
        num -> num.intValue() - 1,
        num -> num.doubleValue() * 3.14
        ); // just an example list here 

public double getResult() {
    double result = 0.0;

    for (int i = 0; i < operations.size(); i++) {
        if (i == 0) {
            result = operations.get(i).apply(initialValue).doubleValue();
        } else {
            result += operations.get(i).apply(result).doubleValue();
        }
    }
    return result;
}
2 of 2
0

I'm not convinced I understand correctly, but I think the problem you're facing is how to call the functions in your List? The JavaDoc for the Function interface states that it has a single non-abstract method, apply() that you call to use the Function, as follows:

public double getResult(){
    double result = 0.0;

    for(int i = 0; i < operations.size(); i++){
        if(i == 0)
            result = operations.get(i).apply(initialValue);
        else
            result += operations.get(i).apply(result);
    }
    return result;
}

As an aside, that method could be tidied up a bit to make it simpler:

public double getResult() {
    double result = initialValue;

    //Not sure if this if-statement is a requirement, but it is to replicate
    //the functionality in the question
    if (operations.isEmpty()) {
        return 0.0;
    }

    for (Operation operation : operations) {
        result = operation.apply(result);
    }

    return result;
}

And as others have said in the comments, you should use generics when passing around your List objects (I guess you'll have to use something like List<Function<? extends Number, ? extends Number>)

Top answer
1 of 2
4

In Java, this is the best you can hope for. For each new function, you must make an additional change to ensure that it gets called. While you could pass an array of Functions to prevent setting up each individual call, you still need to alter the array with each successive function created, so it is ultimately no better.

Though if you're like me, you may want to categorize these functions so that you can have a single comprehensive list. While it doesn't reduce the amount of changes you'll need to make to the code, this way I find is more organized.

public enum TestFunctions {
    BUBBLE_SORT(SortingAlgorithms::bubbleSort),
    QUICK_SORT(SortingAlgorithms::quickSort),
    MERGE_SORT(SortingAlgorithms::mergeSort),
    INSERTION_SORT(SortingAlgorithms::insertionSort);

    UnaryOperator<Integer[]> function;

    private TestFunctions(UnaryOperator<Integer[]> function) {
        this.function = function;
    }

    public Integer[] call(Integer[] input) {
        return this.function.apply(input);
    }
}

Like this, your code can separate the function from where it's implemented. You'd still need to add the new function here, but there's no getting around this unless you used reflection. Though you could use reflection, I would strongly discourage you from doing so. My experience is that it tends to create more problems than it solves in the long run.

Good luck!

2 of 2
2

If you want to create a list of functions, that is very easy. You literally just need to create a List of Functions. Example:

import java.util.List;
import java.util.function.Function;

class Test {
  private static List<Function<int[], int[]>> functions = List.of(
      a -> new int[] { a[0] + a[1] },
      a -> new int[] { a[0] - a[1] }
  );

  public static void main(String[] args) {
    for (var f : functions) {
      System.out.println(f.apply(new int[] { 1, 2 })[0]);
    }
  }
}

// 3
// -1
🌐
iO Flood
ioflood.com › blog › java-list-methods
Java List Methods Explained: From Basics to Advanced
February 27, 2024 - In this example, we create a new ArrayList and add the string ‘Hello’ to it using the add() method. We then retrieve the first item in the list using the get() method, which returns ‘Hello’. This is just a basic introduction to Java List methods, but there’s much more to learn about these versatile tools.
Find elsewhere
🌐
Software Testing Help
softwaretestinghelp.com › home › java › java list methods – sort list, contains, list add, list remove
Java List Methods - Sort List, Contains, List Add, List Remove
April 1, 2025 - This Tutorial Explains Various Java List Methods such as Sort List, List Contains, List Add, List Remove, List Size, AddAll, RemoveAll, Reverse List & More:
🌐
IONOS
ionos.com › digital guide › websites › web development › java list
What is a Java list? - IONOS
November 3, 2023 - Lists are one of the fun­da­men­tal data struc­tures in Java pro­gram­ming and can be used in a variety of ways. They contain elements, which are arranged in an ordered sequence. You can add, modify, delete or query elements in a list.
🌐
Amigoscode
blog.amigoscode.com › p › 18-most-used-java-list-methods
18 Most Used Java List Methods
December 14, 2023 - 3. remove(Object o) - Removes the first occurrence of the specified element from the list.
🌐
University of Washington
courses.cs.washington.edu › courses › cse341 › 98au › java › jdk1.2beta4 › docs › api › java › util › List.html
Interface java.util.List
Unlike Sets, Lists typically allow duplicate elements. More formally, Lists typically allow pairs of elements e1 and e2 such that e1.equals(e2), and they typically allow multiple null elements if they allow null elements at all.
🌐
W3Schools
w3schools.com › java › java_ref_arraylist.asp
Java ArrayList Reference
Java Examples Java Videos Java Compiler Java Exercises Java Quiz Java Code Challenges Java Server Java Syllabus Java Study Plan Java Interview Q&A Java Certificate ... A list of all ArrayList methods can be found in the table below.
🌐
Programiz
programiz.com › java-programming › library
Java Library Functions | Programiz
This page contains all methods in Python Standard Library: built-in, dictionary, list, set, string and tuple.
🌐
Medium
medium.com › @AlexanderObregon › javas-list-of-method-explained-e21e66f97e82
Java's List.of() Method Explained | Medium
December 4, 2024 - Learn how Java's List.of() method creates immutable lists, its advantages for constant data, and how it compares with mutable lists using practical examples.
Top answer
1 of 2
22

This has just been asked a few hours ago for a Consumer... You could reduce them to a single function and apply that:

@SafeVarargs
private static <T> Function<T, T> combineF(Function<T, T>... funcs) {
    return Arrays.stream(funcs).reduce(Function.identity(), Function::andThen);
}
2 of 2
2

Here's a variant on Eugene's answer, just for fun:

public static <T> Function<T, T> combine(List<Function<T, T>> functions) {
    return new Object() {
        Function<List<Function<T, T>>, Function<T, T>> combiner = list ->
            list.size() == 1 ? list.get(0) :
            list.get(0).andThen(this.combiner.apply(list.subList(1, list.size())));
    }.combiner.apply(functions);
}

This creates an anonymous inner class with an attribute that is a recursive lambda. This attribute is named combiner and it's a higher-order function that takes a list of functions as an argument and returns a function as a result. This higher-order function returns the first function of the list if the list contains only one element, or applies andThen to the first function of the list, with the function that results of the recursive call to the higher-order function with the sublist of functions that starts with the second element.

The anonymous inner class is needed because recursive lambdas can only be defined as attributes of a class.

Needless to say this is way more complex than streaming the list and reducing with the Function::andThen binary operator. Besides, recursive lambdas aren't for free: they use the stack for the recursive calls.

🌐
Notepad++ Community
community.notepad-plus-plus.org › topic › 25147 › better-function-list-for-java
Better Function List for Java ? | Notepad++ Community
November 18, 2023 - Our resident Function List guru, @MAPJe71 , who wrote our FAQ on Function List, has a collection of languages that have Function List definitions and/or autoCompletion definitions for quite a few programming languages – but unfortunately his Java entry only has autoCompletion, not Function List.
🌐
W3Schools
w3schools.com › JAVA › java_list.asp
Java List
Java Examples Java Videos Java ... ... The List interface is part of the Java Collections Framework and represents an ordered collection of elements....
🌐
W3Schools
w3schools.com › java › java_methods.asp
Java Methods
Java Data Structures Java Collections Java List Java ArrayList Java LinkedList Java List Sorting Java Set Java HashSet Java TreeSet Java LinkedHashSet Java Map Java HashMap Java TreeMap Java LinkedHashMap Java Iterator Java Algorithms
🌐
Learn Java
learnjavaonline.org › en › Functions
Functions - Learn Java - Free Interactive Java Tutorial
In Java, all function definitions must be inside classes. We also call functions methods.
🌐
Javatpoint
javatpoint.com › java-list
Java List Interface - javatpoint
java List Interface with methods and examples, ListIterator interface extends the List interface.This interface provides the facility to traverse the elements in backward and forward direaction.