引子
- 只包含一个抽象方法的接口,就称为函数式接口。
- 来源:java.util.function
我想在方法内直接定义方法直接获得结果,主要是也是为了配置lambda表达式进行操作,我在项目代码中引入了如下code(对代码进行保密处理)
private Device DDDD(JSONObject json) {
Device device = new Device();
device.setCode((String) json.get(A.Code));
device.setCategoryCode((String) json.get(A.CategoryCode));
device.setDeviceCode((String) json.get(A.deviceCode));
device.setDeviceNum(Long.parseLong(json.get(A.DeviceNum).toString()) );
return device;
}
通过函数式接口进行改写
BiConsumer、BiFunction、BiPrediate 是 Consumer、Function、Predicate 的扩展,可以传入多个参数,没有 BiSupplier 是因为 Supplier 没有入参。
private Device DDDD(JSONObject json) {
Device device = new Device();
List<Object> objectsList = Arrays.asList(
json.get(A.Code),
json.get(A.CategoryCode),
json.get(A.deviceCodeName),
json.get(A.DeviceNum));
BiConsumer<List<Object>,Device> consumer = (List<Object> list, Device d) -> {
d.setCode((String) list.get(0));
d.setCategoryCode((String) list.get(1));
d.setDeviceCode((String) list.get(2));
d.setDeviceNum(Long.parseLong(list.get(3).toString()));
};
consumer.accept(objectsList, device);
return device;
}
ok,测试成功
函数式接口,即适用于函数式编程场景的接口。而Java中的函数式编程体现就是 Lambda,所以函数式接口就是可以适用于Lambda使用的接口。
四种函数式接口-简单Demo
给出一个简单demo进行了解
public static void main(String[] args) {
int ss = 0;
Consumer consumer = s -> {
s = (int) s + 1;
System.out.println("ss" + s);
};
consumer.accept(ss);
String str = "aa";
Consumer strC = s -> {
s = s + "1";
System.out.println("str " + s);
};
strC.accept(str);
System.out.println(str);
Function<String, String> function = (s) -> {
s = s + "1";
System.out.println("---function---");
return s;
};
str = function.apply(str);
System.out.println(str);
Predicate predicate = (s)->{
return s.equals("aa1");
};
System.out.println(predicate.test(str));
}
结果展示如下
四种函数式接口介绍
Consumer<T>
消费型接口
void accept(T t) 接收一个参数进行消费,无返回结果 (因为没有返回值,接口里面执行内容和调用方没什么关联 – 解耦)
// 通过Consumer 消费输出传入的参数
Consumer consumer= i -> System.out.println(i);
consumer.accept("test consumer accept");
Supplier<T>
供给型接口
T get() 返回一个自定义数据( 无参数,有返回值)
// 通过Supplier 创建一个字符串
Supplier<String> supplier= () -> new String("test Supplier get");
String str = supplier.get();
System.out.println(str);
Function<T,R>
函数型接口
R apply(T t) 传入一个参数,返回需要的结果
//Function<参数, 返回值>
Function<String, String> nameFunction = (i) -> "hello, name = { " + i + " }";
Function<Integer, String> ageFunction = (i) -> "age = { " + i + " }";
String name = nameFunction.apply("Tom");
String age = ageFunction.apply(21);
System.out.println("ageFunction = " + name);
System.out.println("ageFunction = " + age);
//先执行ageFunction,然后将得到的结果传给f1执行。
String ageName = nameFunction.compose(ageFunction).apply(21);
System.out.println("ageName = " + ageName);
//获得输入参数意义的结果字符串
String identity = Function.identity().apply(" identity ") + "生成";
System.out.println("identity = " + identity);
Predicate<T,R>
断言型接口
boolean test(T t) 传入一个参数,返回布尔值(满足:true,不满足:false)
// equals:判断传入参数是否等价于strive
Predicate<String> v1 = (i) -> "strive".equals(i);
//endsWith:判断字符串是否以指定的后缀结束
Predicate<String> v2 = (i) -> i.endsWith("ive");
boolean test = v1.test("strive");
System.out.println("test = " + test);
// isEqual : 当参数为null,使用==判断,否则使用equal方法判断
Predicate<String> p = Predicate.isEqual("strive");
boolean isEqual = p.test("strive");
System.out.println("isEqual = " + isEqual);
boolean negate = v1.negate().test("hello");
System.out.println("negate = " + negate);
//等价于 v2.test(arg) && v1.test(arg)
boolean and = v1.and(v2).test("strive");
System.out.println("and = " + and);
//等价于 v2.test(arg) || v1.test(arg)
boolean or = v1.or(v2).test("hello");
System.out.println("or = " + or);
函数式接口实战-代码对比
private String[] get(Map<String, String> pM) {
int rrLen = 0;
for (int i = 0; i < CV.InfoBoardNum; i++) {
String content = pM.get(CV.InfoBoardContent + i);
if (!StringUtils.isEmpty(content)) {
rrLen++;
}
}
if (rrLen>0){
String[] numStr = new String[rrLen];
int CCCn = 0;
for (int i = 0; i < CV.InfoBoardNum; i++) {
//这里需要确定对应的点位是否正确
String content = pM.get(CV.InfoBoardContent + i);
if (!StringUtils.isEmpty(content)) {
numStr[CCCn++] = String.valueOf(i);
}
}
return numStr;
}
return null;
}
重写后
private String[] get(Map<String, String> pM) {
int rrLen = 0;
Boolean b = false;
String[] numStr = null;
Function<List, Object> function = (l) -> {
int rl = (int) l.get(0);
Boolean flag = (Boolean) l.get(1);
String[] numS = (String[]) l.get(2);
for (int i = 0; i < CV.InfoBoardNum; i++) {
String content = pM.get(CV.InfoBoardContent + i);
if (!StringUtils.isEmpty(content)) {
if(flag){numS[rl++] = String.valueOf(i);}
else {rl++;}
}
}
if(flag){return rl;}
else {return numS;}
};
List list = new ArrayList();
list.add(rrLen);
list.add(b);
list.add(numStr);
rrLen = (int) function.apply(list);
if (rrLen>0){
list = new ArrayList();
numStr = new String[rrLen];
int CCCn = 0;
b = true;
list.add(CCCn);
list.add(b);
list.add(numStr);
return (String[]) function.apply(list);
}
return null;
}
关于Consumer赋值问题(疑问,求解答)
Integer ss = 0;
Consumer consumer1 = s -> {
s = (Integer) s + 1;
System.out.println("ss" + s);
};
System.out.println("最后结果1: "+ss);
consumer1.accept(ss);
System.out.println("最后结果2: "+ss);
System.out.println("-------------------");
这里我一开始以为是包装类型和基本类型问题,我一开始测试的是int类型,后面发现Integer也无法成功,
所以又 测试了个JAVABEAN
再对比下另一个
List list1 = new ArrayList();
list1.add(1);
list1.add("king");
BiConsumer<List<Object>,Student> consumer = (List<Object> list, Student d) -> {
d.setId((int) list.get(0));
d.setName((String) list.get(1));
};
Student student = new Student();
System.out.println(student);
consumer.accept(list1, student);
System.out.println(student);
System.out.println("-------------------");
System.out.println("-------------------");
Student student1 = new Student();
Consumer consumer2 = s -> {
student1.setId(2);
System.out.println("ss" + s);
};
System.out.println("最后结果1: "+student1);
consumer2.accept(ss);
System.out.println("最后结果2: "+student1);
但是我
System.out.println("-------------------");
AtomicReference<Integer> ss = new AtomicReference<>(0);
ss.set(0);
Consumer consumer3 = s -> {
ss.set(1);
// System.out.println("ss" + s);
};
consumer3.accept(ss);
System.out.println(ss);
其实consumer实现了一个内部操作功能,但是对于Bean类型赋值,是成功的,但是对于int、Integer类型赋值,只会在内部实现,外界得不到操作后的结果。
这是我的疑惑吧
此问题已经在给出答案
位置:
链接: Java的值传递