You can't use a method reference, since you have no way of passing the int argument to it.

Therefore, use a lambda expression instead:

lines.forEach(s -> chunker.splitFileByMaxRows(s,someInt));
Answer from Eran on Stack Overflow
🌐
Java67
java67.com › 2016 › 01 › how-to-use-foreach-method-in-java-8-examples.html
10 Examples of forEach() method in Java 8 | Java67
Now you know a little bit about the forEach() method and Java 8, it's time to see some code examples and explore more of the forEach() method in JDK 8. You can loop over all elements using the Iterable.forEach() method as shown below: List<String> alphabets = new ArrayList<>(Arrays.asList("aa", "bbb", "cat", "dog")); alphabets.forEach(s -> System.out.println(s)); This code will print every element of the list called alphabets. You can even replace lambda expression with method reference because we are passing the lambda parameter as it is to the System.out.println() method as shown below:
🌐
Baeldung
baeldung.com › home › java › core java › guide to the java foreach loop
Guide to the Java forEach Loop | Baeldung
June 17, 2025 - Introduced in Java 8, the forEach() method provides programmers with a concise way to iterate over a collection.
🌐
HowToDoInJava
howtodoinjava.com › home › java 8 › java foreach()
Java forEach() with Examples - HowToDoInJava
February 6, 2023 - The forEach() method performs the given action for each element of the List (or Set) until all elements have been processed or the action throws an exception.
🌐
Javatpoint
javatpoint.com › java-8-foreach
Java 8 forEach() Method
The java.lang.reflect package contains all the required classes like Method and Parameter to work with parameter reflection. Method class It provides information about single method on a... ... Oracle released a new version of Java as Java 8 in March 18, 2014.
Find elsewhere
🌐
Tpoint Tech
tpointtech.com › java-8-foreach
Java 8 forEach() method - Tpoint Tech
June 28, 2026 - (Consumer<? super T> action): It is a parameter of the method. It is a functional interface belongs to the java.util.function package. It accepts an argumentand returns no result. We usually pass a lambda expression · The following Java program demonstrates how to use Java 8's forEach() method with a lambda expression to iterate through an ArrayList.
Top answer
1 of 3
24

There are three kinds of method references that can be used when a single value is available from the stream:

  1. A parameter-less method of the streamed object.

    class Observer {
        public void act() {
            // code here
        }
    }
    
    observers.forEach(Observer::act);
    
    observers.forEach(obs -> obs.act()); // equivalent lambda
    

    The streamed object becomes the this object of the method.

  2. A static method with the streamed object as parameter.

    class Other {
        public static void act(Observer o) {
            // code here
        }
    }
    
    observers.forEach(Other::act);
    
    observers.forEach(obs -> Other.act(obs)); // equivalent lambda
    
  3. A non-static method with the streamed object as parameter.

    class Other {
        void act(Observer o);
    }
    
    Other other = new Other();
    observers.forEach(other::act);
    
    observers.forEach(obs -> other.act(obs)); // equivalent lambda
    

There is also a constructor reference, but that is not really relevant to this question.

Since you have an external value temp, and you want to use a method reference, you can do the third option:

class Temp {
    private final BigDecimal temp;
    public Temp(BigDecimal temp) {
        this.temp = temp;
    }
    public void apply(TemperatureObserver observer) {
        observer.react(this.temp);
    }
}

Temp tempObj = new Temp(temp);

observers.forEach(tempObj::apply);
2 of 3
9

Take a look at the Method References section in the Java Tutorial. There it says:

There are four kinds of method references:

  • Reference to a static method: ContainingClass::staticMethodName

  • Reference to an instance method of a particular object: containingObject::instanceMethodName

  • Reference to an instance method of an arbitrary object of a particular type: ContainingType::methodName

  • Reference to a constructor: ClassName::new

There it explains that i.e. TemperatureObserver::react would be a method reference of the 3rd type: a reference to an instance method of an arbitrary object of a particular type. In the context of your call to the Stream.forEach method, that method reference would be equivalent to the following lambda expression:

(TemperatureObserver item) -> item.react()

Or just:

item -> item.react()

Which doesn't match your void TemperatureObserver.react(BigDecimal t) method signature.

As you already suspect, there are cases for which you can't find an equivalent method reference for a lambda. Lambdas are way more flexible, though IMHO sometimes they are less readable than method references (but this is a matter of taste, many people think the other way round).

A way to still use a method reference would be with a helper method:

public static <T, U> Consumer<? super T> consumingParam(
        BiConsumer<? super T, ? super U> biConsumer,
        U param) {

    return t -> biConsumer.accept(t, param);
}

Which you could use as follows:

observers.forEach(consumingParam(TemperatureObserver::react, temp));

But, honestly, I prefer to use a lambda.

🌐
Mkyong
mkyong.com › home › java8 › java 8 foreach examples
Java 8 forEach examples | mkyong.com
December 4, 2020 - It is equals the– items.forEach(item->System.out.println(item)); –? Because the parameter and the return type is the same? Sorry for dump question . Reply Helpful Report Spam or advertising Abusive or offensive Off topic · mkyong Author · December 4, 2020 at 12:48 pm · yes, method reference · https://mkyong.com/java8/java-8-method-references-double-colon-operator/ Reply Helpful Report Spam or advertising Abusive or offensive Off topic ·
🌐
Blogger
javarevisited.blogspot.com › 2015 › 09 › java-8-foreach-loop-example.html
Java 8 forEach() Loop Example
listOfPrimes.stream().forEach(System.out::println); You can use method reference in place of the lambda expression if you are not doing anything with the parameter, except passing it to another method. For example, in our next two examples, we cannot replace lambda expression with method reference because we are modifying the element. You can also see Java SE 8 for Really Impatient to learn more about method reference and lambda expression, one of the better books to learn Java. You can even apply other useful stream methods before calling forEach() e.g.
🌐
Spring Framework Guru
springframework.guru › home › java 8 foreach
Java 8 forEach - Spring Framework Guru
May 20, 2019 - The forEach method performs the given action for each element of the Iterable until all elements have been processed or the action throws an exception. The Consumer parameter of forEach is a functional interface with the accept(Object) method.
🌐
EDUCBA
educba.com › home › software development › software development tutorials › java 8 tutorial › java 8 foreach
Java 8 forEach | Default Method Defined in the Iterable Interface.
February 14, 2023 - Collection classes extend the iterable interface, which is used to iterate the elements in a foreach loop. The foreach method in Java 8 accepts a single parameter from the functional interface, allowing us to pass a lambda expression.
Address: Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
Java Code Geeks
examples.javacodegeeks.com › home › java development › core java
For Each Loop Java 8 Example - Java Code Geeks
April 29, 2021 - Java 8 came up with a new feature to iterate over the Collection classes, by using the forEach() method of the Iterable interface or by using the new Stream class.
🌐
Baeldung
baeldung.com › home › java › java list › call a method on each element of a list in java
Call a Method on Each Element of a List in Java | Baeldung
March 7, 2025 - In Java 8, the forEach() method is a new method in the Iterable interface: default void forEach(Consumer<? super T> action) { Objects.requireNonNull(action); for (T t : this) { action.accept(t); } } As we can see, forEach() wraps the loop implementation and makes the code on the caller’s side easier to read.