java lambda 接口.demo_Java 函数式接口 lambda

importjava.io.Serializable;importjava.util.ArrayList;importjava.util.List;public classDemo1 {public static voidmain(String[] args) {//使用lambda表达式,如runnable

Thread th1 = new Thread(() ->{

System.out.println("lambda 创建线程"+Thread.currentThread().getName());

});

th1.start();/*1、说明:()->{} 这里的()是参数列表,{}是执行主体,->分割参数列表和执行主体

* 2、那么什么函数可以使用lambda表达式呢?

* 答:函数式接口。

* 3、什么是函数式接口?

* 答:只定义一个抽象方法的接口。比如Runnable、Callable、Comparator

* 4、如何检查函数式接口?

* 答:JDK8中提供了@FunctionInterface注解来专门表示函数式接口,用来对接口的声明进行语法检查。*/

//自定义函数式接口

User user1=newUser();

user1.setName("王一");

user1.setAge(10);

user1.setSex("女");

User user2=newUser();

user2.setName("王二");

user2.setAge(20);

user2.setSex("男");

User user3=newUser();

user3.setName("王三");

user3.setAge(30);

user3.setSex("女");

List users=new ArrayList();

users.add(user1);

users.add(user2);

users.add(user3);//查询年龄在15-30之间的用户

List res=User.userFilter(users, user->user.getAge()>15&&user.getAge()<30);

System.out.println("年龄在15-30之间的用户:"+res);//查询性别是女的用户

List res2=User.userFilter(users, user->user.getSex().equals("女"));

System.out.println("性别是女的用户:"+res2);//查询年龄在15-30之间的用户--泛型

List res3=FuncInterfaceTWarp.filterT(users,t->((User)t).getAge()>15&&((User)t).getAge()<30);

System.out.println("年龄在15-30之间的用户--泛型:"+res3);//查询性别是女的用户--泛型

List res4=FuncInterfaceTWarp.filterT(users,t->((User)t).getSex().equals("女"));

System.out.println("性别是女的用户--泛型:"+res4);

}

}/*** 用户实体

**/

class User implementsSerializable{private static final long serialVersionUID = 1L;privateString name;privateInteger age;privateString sex;publicString getName() {returnname;

}public voidsetName(String name) {this.name =name;

}publicInteger getAge() {returnage;

}public voidsetAge(Integer age) {this.age =age;

}publicString getSex() {returnsex;

}public voidsetSex(String sex) {this.sex =sex;

}

@OverridepublicString toString() {return "User [name=" + name + ", age=" + age + ", sex=" + sex + "]";

}public static List userFilter(Listusers,FuncInterface funcInterface){

List res=new ArrayList();for(User user:users){if(funcInterface.execute(user)){

res.add(user);

}

}returnres;

}

}/*** 定义函数式接口

*@authorAdministrator

**/@FunctionalInterfaceinterfaceFuncInterface{booleanexecute(User user);

}/*** 定义泛型函数式接口

**/@FunctionalInterfaceinterface FuncInterfaceT{booleanexecute(T t);

}/*** 定义泛型函数式接口包装类

**/

classFuncInterfaceTWarp{public static List filterT(List objs,FuncInterfaceTfuncInterfaceT){

List res=new ArrayList<>();for(T t:objs){if(funcInterfaceT.execute(t)){

res.add(t);

}

}returnres;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面这串代码什么作用self.det_thread = DetThread() self.model_type = self.comboBox.currentText() self.det_thread.weights = "./pt/%s" % self.model_type self.det_thread.source = '0' self.det_thread.percent_length = self.progressBar.maximum() self.det_thread.send_raw.connect(lambda x: self.show_image(x, self.raw_video)) self.det_thread.send_img.connect(lambda x: self.show_image(x, self.out_video)) self.det_thread.send_statistic.connect(self.show_statistic) self.det_thread.send_msg.connect(lambda x: self.show_msg(x)) self.det_thread.send_percent.connect(lambda x: self.progressBar.setValue(x)) self.det_thread.send_fps.connect(lambda x: self.fps_label.setText(x)) self.fileButton.clicked.connect(self.open_file) self.cameraButton.clicked.connect(self.chose_cam) self.rtspButton.clicked.connect(self.chose_rtsp) self.runButton.clicked.connect(self.run_or_continue) self.stopButton.clicked.connect(self.stop) self.comboBox.currentTextChanged.connect(self.change_model) self.confSpinBox.valueChanged.connect(lambda x: self.change_val(x, 'confSpinBox')) self.confSlider.valueChanged.connect(lambda x: self.change_val(x, 'confSlider')) self.iouSpinBox.valueChanged.connect(lambda x: self.change_val(x, 'iouSpinBox')) self.iouSlider.valueChanged.connect(lambda x: self.change_val(x, 'iouSlider')) self.rateSpinBox.valueChanged.connect(lambda x: self.change_val(x, 'rateSpinBox')) self.rateSlider.valueChanged.connect(lambda x: self.change_val(x, 'rateSlider')) self.checkBox.clicked.connect(self.checkrate) self.saveCheckBox.clicked.connect(self.is_save) self.load_setting()
06-03

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值