?應用場景:
第一次程序員A寫好了個基礎的遍歷方法:
public class Demo1 {public static void main(String[] args) {//假設main方法為程序員B寫的,此時需要去調用A寫好的一個遍歷方法//1.如果此時B突然發現想將字符串以小寫的形式打印出來,則去請求A修改LinkedList<String > list=new LinkedList<>();list.add("Acb");list.add("Vudh");list.add("hsuU");print(list);}//A程序員寫的遍歷方法public static void print(LinkedList<String> list){for (String s : list) {System.out.println(s);}}
}
?第二次程序員A完成了字符轉小寫的遍歷方法:
public static void main(String[] args) {//假設main方法為程序員B寫的,此時需要去調用A寫好的一個遍歷方法//1.如果此時B突然發現想將字符串以小寫的形式打印出來,則去請求A修改LinkedList<String > list=new LinkedList<>();list.add("Acb");list.add("Vudh");list.add("hsuU");print(list);}//A程序員寫的遍歷方法public static void print(LinkedList<String> list){for (String s : list) {System.out.println(s.toLowerCase());}}
第三次程序員A直接將接口調用給了B,B可以根據需求自己去實現打印形式:
public static void main(String[] args) {//假設main方法為程序員B寫的,此時需要去調用A寫好的一個遍歷方法//1.如果此時B突然發現想將字符串以小寫的形式打印出來,則去請求A修改LinkedList<String > list=new LinkedList<>();list.add("Acb");list.add("Vudh");list.add("hsuU");print(list, new Consumer<String>() {@Overridepublic void accept(String s) {System.out.println(s.toUpperCase());}});}//A程序員寫的遍歷方法,將consumer接口調用給Bpublic static void print(LinkedList<String> list,Consumer<String> consumer ){for (String s : list) {consumer.accept(s);}}
內容補充:
????????
- 定義與概念
- 在 Java 中,
Consumer
接口是一個函數式接口,它屬于java.util.function
包。函數式接口是指只包含一個抽象方法的接口,Consumer
接口中的抽象方法是void accept(T t)
。這個方法接收一個類型為T
的參數,并且沒有返回值。它主要用于對給定的輸入參數進行某種操作,比如打印、修改等,但不返回結果。
- 在 Java 中,
- 使用場景
- 數據消費:可以用于遍歷一個集合,對集合中的每個元素進行某種操作。例如,有一個包含整數的列表,想要打印出列表中的每個元素,可以使用
Consumer
接口。 - 對象屬性修改:在面向對象編程中,用于修改對象的某些屬性。比如,有一個
Person
類,包含name
和age
屬性,通過Consumer
接口可以修改Person
對象的age
屬性。
- 數據消費:可以用于遍歷一個集合,對集合中的每個元素進行某種操作。例如,有一個包含整數的列表,想要打印出列表中的每個元素,可以使用
- 示例代碼
- 簡單的打印操作
import java.util.ArrayList; import java.util.List; import java.util.function.Consumer; public class ConsumerExample {public static void main(String[] args) {List<Integer> numberList = new ArrayList<>();numberList.add(1);numberList.add(2);numberList.add(3);// 使用Consumer接口打印列表中的每個元素Consumer<Integer> printConsumer = num -> System.out.println(num);numberList.forEach(printConsumer);} }
- 在這個示例中,首先創建了一個包含整數的
ArrayList
。然后定義了一個Consumer
類型的變量printConsumer
,它的accept
方法實現是通過 Lambda 表達式num -> System.out.println(num)
來定義的,即對于傳入的整數進行打印操作。最后,使用forEach
方法遍歷numberList
,并將每個元素傳遞給printConsumer
的accept
方法進行打印。 - 對象屬性修改示例
class Person {String name;int age;public Person(String name, int age) {this.name = name;this.age = age;}public String getName() {return name;}public int getAge() {return age;} } public class ConsumerObjectExample {public static void main(String[] args) {Person person = new Person("Alice", 20);// 定義一個Consumer來修改Person對象的年齡Consumer<Person> ageModifier = p -> p.age = p.age + 1;ageModifier.accept(person);System.out.println("修改后的年齡: " + person.getAge());} }
- 在這里,定義了一個
Person
類,包含name
和age
屬性。在main
方法中創建了一個Person
對象。然后定義了一個Consumer
,它的accept
方法實現是將傳入的Person
對象的年齡加 1。最后通過accept
方法修改person
對象的年齡,并打印出修改后的年齡。
- 簡單的打印操作
- 與其他函數式接口的比較
- 與
Function
接口相比,Function
接口有返回值,其抽象方法是R apply(T t)
,R
是返回值類型,而Consumer
接口沒有返回值。例如,Function
可以用于將一個整數列表中的每個元素進行平方運算并返回新的列表,Consumer
則更側重于對元素進行操作而不返回新的計算結果。 - 與
Supplier
接口不同,Supplier
接口沒有輸入參數,其抽象方法是T get()
,主要用于提供一個對象或值,而Consumer
是消費一個已經存在的對象或值。
- 與
用Java的Consumer接口實現一個統計列表中元素個數的程序
在Java 8中,如何使用Consumer接口與Stream API結合?
除了Consumer接口,Java中還有哪些常用的函數式接口?