# BiConsumer用法 ```java= public static void main(String[] args) { Map<Integer,Integer> mVal = new HashMap<>(); mVal.put(1, 10); mVal.put(2, 20); BiConsumer<Integer,Integer> c = (i,j)->{ System.out.print(i+","+j+";"); }; c.accept(1, 2); mVal.forEach(c); } ``` Console: ```Console= 1,2;1,10;2,20; ``` ## Which statement can be inserted into line n1 to print 1,2; 1,10; 2,20;? **A. BiConsumer<Integer,Integer> c = (i, j) -> {System.out.print (i + "," + j+ "; ");};** B. BiFunction<Integer, Integer, String> c = (i, j) ?gt; {System.out.print (i + "," + j+ "; ")}; C. BiConsumer<Integer, Integer, String> c = (i, j) ?gt; {System.out.print (i + "," + j+ "; ")}; D. BiConsumer<Integer, Integer, Integer> c = (i, j) ?gt; {System.out.print (i + "," + j+ "; ");}; - [x] **Answer: A** :::warning 選項A: 正確用法 選項B~選項D:參數只能輸入兩個 ::: :::info BiConsumer 有兩個泛型參數,跟Consumer一樣,都有一個accept方法。 BiConsumer的accept方法會先執行,然後才是傳進去的參數 ::: ###### tags: `ocpjp`