openplanning

Hướng dẫn và ví dụ Java BiConsumer

  1. BiConsumer
  2. BiConsumer + Method reference
  3. andThen(BiConsumer after)
  4. Example: Map to Map

1. BiConsumer

Trong Java 8, BiConsumer là một functional interface, đại diện cho một toán tử chấp nhận hai tham số đầu vào và không trả về gì cả.
BiConsumer
@FunctionalInterface
public interface BiConsumer<T, U> {
  void accept(T t, U u);

  default BiConsumer<T, U> andThen(BiConsumer<? super T, ? super U> after);
}
Xem thêm: Consumer là một interrface tương tự BiConsumer, nhưng nó chỉ chấp nhận một tham số:
Ví dụ:
BiConsumerEx1.java
package org.o7planning.biconsumer.ex;

import java.util.function.BiConsumer;

public class BiConsumerEx1 {

    public static void main(String[] args) {
        
        // Create a BiConsumer object directly
        BiConsumer<String, String> greeter
             = (firstName, lastName) -> System.out.println("Hello " + firstName+ " " + lastName);
         
        greeter.accept("James", "Smith");
    }
}
Output:
Hello James Smith
Dưới đây là danh sách các phương thức trong package java.util sử dụng BiConsumer:
Modifier and Type
Method and Description
void
IdentityHashMap.forEach(BiConsumer<? super K,? super V> action)
void
TreeMap.forEach(BiConsumer<? super K,? super V> action)
void
LinkedHashMap.forEach(BiConsumer<? super K,? super V> action)
void
Hashtable.forEach(BiConsumer<? super K,? super V> action)
void
HashMap.forEach(BiConsumer<? super K,? super V> action)
void
WeakHashMap.forEach(BiConsumer<? super K,? super V> action)
default void
Map.forEach(BiConsumer<? super K,? super V> action)
Ví dụ: Sử dụng phương thức Map.forEach(BiConsumer):
BiConsumerEx2.java
package org.o7planning.biconsumer.ex;

import java.util.HashMap;
import java.util.Map;
import java.util.function.BiConsumer;

public class BiConsumerEx2 {

    public static void main(String[] args) {
         
        BiConsumer<String, String> printer
             = (phoneNumber, fullName) -> System.out.println(phoneNumber + " - " + fullName);
       
       Map<String, String> contacts = new HashMap<String,String>();
       contacts.put("1111 2222", "James Smith");
       contacts.put("1111 3333", "Michael Smith");
       contacts.put("1233 5555", "David Garcia");
       
       contacts.forEach(printer);
    }  
}
Output:
1233 5555 - David Garcia
1111 2222 - James Smith
1111 3333 - Michael Smith

2. BiConsumer + Method reference

Nếu một phương thức tĩnh có hai tham số và không trả về gì cả, thì tham chiếu của nó có thể coi như là một BiConsumer.
Ví dụ:
BiConsumer_mRef_ex1.java
package org.o7planning.biconsumer.ex;

import java.util.HashMap;
import java.util.Map;
import java.util.function.BiConsumer;

public class BiConsumer_mRef_ex1 {

    public static void main(String[] args) {  
       Map<String, String> contacts = new HashMap<String,String>();
       contacts.put("1111 2222", "James Smith");
       contacts.put("1111 3333", "Michael Smith");
       contacts.put("1233 5555", "David Garcia");
    
       // Static method reference:
       BiConsumer<String, String> bc = MyUtils::printContactInfo;
           
       contacts.forEach(bc);
    }  
}

class MyUtils {
    // Static method:
    public static void printContactInfo(String phone, String name)  {
        System.out.println("Phone: " + phone + " / Full Name: " + name);
    }
}
Output:
Phone: 1233 5555 / Full Name: David Garcia
Phone: 1111 2222 / Full Name: James Smith
Phone: 1111 3333 / Full Name: Michael Smith
Ví dụ: BiConsumer + Non-static method reference:
BiConsumer_mRef_ex2.java
package org.o7planning.biconsumer.ex;

import java.util.HashMap;
import java.util.Map;
import java.util.function.BiConsumer;

public class BiConsumer_mRef_ex2 {

    public static void main(String[] args) {  
       Map<String, String> contacts = new HashMap<String,String>();
       contacts.put("1111 2222", "James Smith");
       contacts.put("1111 3333", "Michael Smith");
       contacts.put("1233 5555", "David Garcia");
       
       CardTemplate template = new CardTemplate("Designed by Tom");
    
       // Non-static method reference:
       BiConsumer<String, String> bc = template::printCard;
           
       contacts.forEach(bc);
    }  
}

class CardTemplate {
    private String someInfo;
    
    public CardTemplate(String someInfo) {
        this.someInfo = someInfo;
    }
    
    // Non-static Method:
    public void printCard(String phone, String name)  {
        System.out.println("--- ~~~ ---");
        System.out.println("Full Name: " + name);
        System.out.println("Phone: " + phone);
        System.out.println(someInfo);
        System.out.println();
    }
}
Output:
--- ~~~ ---
Full Name: David Garcia
Phone: 1233 5555
Designed by Tom

--- ~~~ ---
Full Name: James Smith
Phone: 1111 2222
Designed by Tom

--- ~~~ ---
Full Name: Michael Smith
Phone: 1111 3333
Designed by Tom

3. andThen(BiConsumer after)

Phương thức andThen(after) trả về một BiConsumer kết hợp. Đầu tiên BiConsumer hiện tại sẽ được gọi sau đó after sẽ được gọi. Nếu lỗi xẩy ra tại một trong 2 bước trên lỗi sẽ được chuyển tới người gọi (caller). Nếu lỗi xẩy ra tại BiConsumer hiện tại thì after bị bỏ qua.
andThen method
default BiConsumer<T, U> andThen(BiConsumer<? super T, ? super U> after) {
    Objects.requireNonNull(after);

    return (l, r) -> {
        accept(l, r);
        after.accept(l, r);
    };
}
Ví dụ:
BiConsumer_andThen_ex1.java
package org.o7planning.biconsumer.ex;

import java.util.HashMap;
import java.util.Map;
import java.util.function.BiConsumer;

public class BiConsumer_andThen_ex1 {

    public static void main(String[] args) {  
       Map<String, String> contacts = new HashMap<String,String>();
       contacts.put("1111 2222", "James Smith");
       contacts.put("1111 3333", "Michael Smith");
       contacts.put("1233 5555", "David Garcia");
       
       BiConsumer<String, String> printer1
           = (phoneNumber, fullName) -> System.out.println(phoneNumber + " - " + fullName);
           
       BiConsumer<String, String> printer2
           = (phoneNumber, fullName) -> System.out.println(phoneNumber + " - " + fullName.toUpperCase());  
           
       contacts.forEach(printer1.andThen(printer2));
    }  
}
Output:
1233 5555 - David Garcia
1233 5555 - DAVID GARCIA
1111 2222 - James Smith
1111 2222 - JAMES SMITH
1111 3333 - Michael Smith
1111 3333 - MICHAEL SMITH

4. Example: Map to Map

Ví dụ: Một đối tượng Map<String,Integer> chứa các ánh xạ giữa mã số nhân viên (employee number) và lương. Tạo một đối tượng Map<String,Integer> tương tự với lương gấp đôi.
BiConsumer_m2m_ex1.java
package org.o7planning.biconsumer.ex;

import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

public class BiConsumer_m2m_ex1 {

    public static void main(String[] args) {
        // String employeeNumber --> Integer salary.
        Map<String, Integer> empSalaryMap = new HashMap<String, Integer>();

        empSalaryMap.put("E01", 1000);
        empSalaryMap.put("E02", 1500);
        empSalaryMap.put("E03", 3000);

        Map<String, Integer> newEmpSalaryMap //
        
                = empSalaryMap.entrySet() // Set<Map.Entry<String,Integer>>
                        .stream() // // Stream<Map.Entry<String,Integer>>
                        // Call method:
                        // collect(Supplier<R>, BiConsumer<R, ? extends Map.Entry<String,Integer>>)
                        .collect(Collectors.toMap(entry -> entry.getKey(), entry -> 2 * entry.getValue()));

        System.out.println("Origin Map: " + empSalaryMap);
        System.out.println();
        System.out.println("New Map: " + newEmpSalaryMap);
    }
}
Output:
Origin Map: {E02=1500, E01=1000, E03=3000}

New Map: {E02=3000, E01=2000, E03=6000}
Về cơ bản, bạn có thể chế biến một đối tượng Map<K,V> để tạo thành một đối tượng Map<K2,V2> khác. Hãy xem thêm bài viết dưới đây:
  • Java Stream.collect method

Java cơ bản

Show More