Close

Java 8 Functional Interfaces - BiConsumer.accept() Examples

Java 8 Functional Interfaces Java Java API 


Interface:

java.util.function.BiConsumer

java.util.function.BiConsumerBiConsumerLogicBig

Method:

void accept(T t, U u)

Performs this operation.

Parameters:
t - the first input argument
u - the second input argument


Examples


package com.logicbig.example.biconsumer;

import java.util.function.BiConsumer;

public class AcceptExample {

public static void main(String... args) {
BiConsumer<String, String> bc = (a, b) -> {System.out.println(a + " | " + b);};
bc.accept("one", "two");
}
}

Output

one | two




BiConsumer can be assigned to any static method reference which takes two arguments. The return value, if there's any, is ignored when assigned to BiConsumer.

package com.logicbig.example.biconsumer;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.function.BiConsumer;

public class AcceptExample2 {

public static void main(String... args) {
BiConsumer<String, String> bc = AcceptExample2::concatAndPrint;
bc.accept("one", "two");

List<List<Integer>> list = List.of(new ArrayList<>(List.of(1, 3)),
new ArrayList<>(List.of(2, 4)));

visitList(list, new Integer[]{10, 12}, Collections::addAll);
System.out.println(list);

visitList(list, 0, Collections::fill);
System.out.println(list);
}

private static void concatAndPrint(String s1, String s2) {
System.out.println(s1 + " | " + s2);
}

private static <T, U> void visitList(List<T> list, U userObject, BiConsumer<T, U> bc) {
list.forEach(t -> bc.accept(t, userObject));
}
}

Output

one | two
[[1, 3, 10, 12], [2, 4, 10, 12]]
[[0, 0, 0, 0], [0, 0, 0, 0]]




BiConsumer<T, X> can be assigned to an instance method reference of type T which takes argument of type X. Such method's return type (if there's any) is ignored when assigned to BiConsumer.

package com.logicbig.example.biconsumer;

import java.util.List;
import java.util.concurrent.atomic.LongAdder;
import java.util.function.BiConsumer;
import java.util.function.Supplier;

public class AcceptExample3 {

public static void main(String... args) {
List<String> list = List.of("one", "|", "two");
StringBuilder sb = visitList(list, StringBuilder::new, StringBuilder::append);
System.out.println(sb.toString());

List<Long> ints = List.of(1L, 3L, 5L, 7L, 9L);

LongAdder la = visitList(ints, LongAdder::new, LongAdder::add);
System.out.println(la.doubleValue());
}

private static <T, U> U visitList(List<T> list, Supplier<U> userObjectSupplier,
BiConsumer<U, T> biConsumer) {
U u = userObjectSupplier.get();
list.forEach(t -> biConsumer.accept(u, t));
return u;
}
}

Output

one|two
25.0




If we have the reference t of a class T which has a method M, the method M takes two argument of types A and B, then BiFunction<A,B> => t::M

package com.logicbig.example.biconsumer;

import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.BiConsumer;

public class AcceptExample4 {
public static void main(String... args) {
List<Integer> list = List.of(1, 3);
AtomicReference<Integer> ai = new AtomicReference<>(3);
visitList(list, Integer.valueOf(10), ai::compareAndExchange);
System.out.println(ai.get());
}

private static <T, U> void visitList(List<T> list, U userObject,
BiConsumer<T, U> biConsumer) {
list.forEach(t -> biConsumer.accept(t, userObject));
}
}

Output

10




See Also