Java 8 | Java中的消费者接口与示例
消费者接口是Java .util 的一部分。从Java 8 开始引入的函数包,用于在Java中实现函数式编程。它表示一个接受一个参数并产生结果的函数。然而,这些类型的函数不返回任何值。
因此,这个功能接口采用一个泛型,即:-
- T :表示操作的输入参数的类型
分配给 Consumer 类型对象的 lambda 表达式用于定义其accept()最终将给定操作应用于其参数。消费者在不需要返回任何值时很有用,因为它们预计会通过副作用进行操作。
消费者接口中的函数
Consumer 接口由以下两个函数组成:
1. 接受()
此方法接受一个值并对给定参数执行操作
句法:
void accept(T t)
参数:此方法接受一个参数:
- t - 输入参数
返回:此方法不返回任何值。
下面是说明 accept() 方法的代码:
方案一:
Java
// Java Program to demonstrate
// Consumer's accept() method
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.function.Consumer;
public class Main {
public static void main(String args[])
{
// Consumer to display a number
Consumer display = a -> System.out.println(a);
// Implement display using accept()
display.accept(10);
// Consumer to multiply 2 to every integer of a list
Consumer > modify = list ->
{
for (int i = 0; i < list.size(); i++)
list.set(i, 2 * list.get(i));
};
// Consumer to display a list of numbers
Consumer >
dispList = list -> list.stream().forEach(a -> System.out.print(a + " "));
List list = new ArrayList();
list.add(2);
list.add(1);
list.add(3);
// Implement modify using accept()
modify.accept(list);
// Implement dispList using accept()
dispList.accept(list);
}
}
Java
// Java Program to demonstrate
// Consumer's andThen() method
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.function.Consumer;
public class Main {
public static void main(String args[])
{
// Consumer to multiply 2 to every integer of a list
Consumer > modify = list ->
{
for (int i = 0; i < list.size(); i++)
list.set(i, 2 * list.get(i));
};
// Consumer to display a list of integers
Consumer >
dispList = list -> list.stream().forEach(a -> System.out.print(a + " "));
List list = new ArrayList();
list.add(2);
list.add(1);
list.add(3);
// using addThen()
modify.andThen(dispList).accept(list);
;
}
}
Java
// Java Program to demonstrate
// Consumer's andThen() method
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.function.Consumer;
public class Main {
public static void main(String args[])
{
// Consumer to multiply 2 to every integer of a list
Consumer > modify = list ->
{
for (int i = 0; i < list.size(); i++)
list.set(i, 2 * list.get(i));
};
// Consumer to display a list of integers
Consumer >
dispList = list -> list.stream().forEach(a -> System.out.print(a + " "));
List list = new ArrayList();
list.add(2);
list.add(1);
list.add(3);
try {
// using addThen()
modify.andThen(null).accept(list);
}
catch (Exception e) {
System.out.println("Exception: " + e);
}
}
}
Java
// Java Program to demonstrate
// Consumer's andThen() method
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.function.Consumer;
public class Main {
public static void main(String args[])
{
// Consumer to multiply 2 to every integer of a list
Consumer > modify = list ->
{
for (int i = 0; i <= list.size(); i++)
list.set(i, 2 * list.get(i));
};
// Consumer to display a list of integers
Consumer >
dispList = list -> list.stream().forEach(a -> System.out.print(a + " "));
System.out.println();
List list = new ArrayList();
list.add(2);
list.add(1);
list.add(3);
// using addThen()
try {
dispList.andThen(modify).accept(list);
;
}
catch (Exception e) {
System.out.println("Exception: " + e);
}
}
}
10
4 2 6
2.然后()
它返回一个组合的消费者,其中参数化的消费者将在第一个消费者之后执行。如果对任一函数的评估引发错误,则会将其转发给组合操作的调用者。
注意:作为参数传递的函数应该是 Consumer 类型。
句法:
default Consumer
andThen(Consumer super T> after)
参数:此方法接受一个参数,在该参数之后是要在当前参数之后应用的消费者。
返回值:此方法返回一个组合的 Consumer,它首先应用当前的 Consumer,然后是 after 操作。
异常:如果 after 操作为 null,则此方法抛出NullPointerException 。
下面是说明 andThen() 方法的代码:
方案一:
Java
// Java Program to demonstrate
// Consumer's andThen() method
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.function.Consumer;
public class Main {
public static void main(String args[])
{
// Consumer to multiply 2 to every integer of a list
Consumer > modify = list ->
{
for (int i = 0; i < list.size(); i++)
list.set(i, 2 * list.get(i));
};
// Consumer to display a list of integers
Consumer >
dispList = list -> list.stream().forEach(a -> System.out.print(a + " "));
List list = new ArrayList();
list.add(2);
list.add(1);
list.add(3);
// using addThen()
modify.andThen(dispList).accept(list);
;
}
}
4 2 6
程序2:演示何时返回NullPointerException。
Java
// Java Program to demonstrate
// Consumer's andThen() method
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.function.Consumer;
public class Main {
public static void main(String args[])
{
// Consumer to multiply 2 to every integer of a list
Consumer > modify = list ->
{
for (int i = 0; i < list.size(); i++)
list.set(i, 2 * list.get(i));
};
// Consumer to display a list of integers
Consumer >
dispList = list -> list.stream().forEach(a -> System.out.print(a + " "));
List list = new ArrayList();
list.add(2);
list.add(1);
list.add(3);
try {
// using addThen()
modify.andThen(null).accept(list);
}
catch (Exception e) {
System.out.println("Exception: " + e);
}
}
}
Exception: java.lang.NullPointerException
程序3:演示after函数中的异常是如何返回和处理的。
Java
// Java Program to demonstrate
// Consumer's andThen() method
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.function.Consumer;
public class Main {
public static void main(String args[])
{
// Consumer to multiply 2 to every integer of a list
Consumer > modify = list ->
{
for (int i = 0; i <= list.size(); i++)
list.set(i, 2 * list.get(i));
};
// Consumer to display a list of integers
Consumer >
dispList = list -> list.stream().forEach(a -> System.out.print(a + " "));
System.out.println();
List list = new ArrayList();
list.add(2);
list.add(1);
list.add(3);
// using addThen()
try {
dispList.andThen(modify).accept(list);
;
}
catch (Exception e) {
System.out.println("Exception: " + e);
}
}
}
2 1 3 Exception: java.lang.IndexOutOfBoundsException: Index: 3, Size: 3