//lambda 函数编程 替代匿名接口实现 //()参数列表 ->连接符 {方法体}
//接口中只许有一个需要实现的接口
1. 简写多线程-run方法
new Thread(new Runnable() {
2.接口实现方法简化
public static void testPerson(Iperson person) {
System.out.println(person.eat("test"));
}
public static void main(String[] args) {
testPerson(new Iperson() {
3.List-sort简化
1.javabean类实现
4.参数省略问题,更加省略写法
-
1.参数列表小括号参数类型可以省略
-
2.参数列表小括号只有一个参数,括号可以省略
-
3.大括号如果也只有一句话,大括号可以省略,return 分号可以省略
new Thread(()->System.out.println(Thread.currentThread().getName())).start();
testPerson(food->2);
Collections.sort(llBeans,(o1,o2)->o2.getAge()-o1.getAge());
5.循环简化
List<TestList> list = new ArrayList<TestList>();
list.add(new TestList("aa", 1));
list.add(new TestList("bb", 0));
Collections.sort(list, (o1,o2)->o1.getOrderNo()-o2.getOrderNo());
for (TestList ll : list) {
System.out.println(ll);
}
list.forEach((TestList ll)->{System.out.println(ll);});
list.forEach(d->System.out.println(d));
6.Map迭代简化
Map<String, Object> map = new TreeMap<String, Object>();
map.put("aa", 11);
map.put("bb",22);
for (Map.Entry<String, Object> entry : map.entrySet()) {
System.out.println(entry.getKey()+":"+entry.getValue());
}
for (String key : map.keySet()) {
System.out.println(key+"::"+map.get(key));
}
map.forEach((key,value)->System.out.println(key+"="+value));
map.forEach((key,value)->{System.out.println(key+"=="+value);});
7.List迭代简化
llBeans.forEach(u->System.out.println(u));
8.Set迭代简化
Set<String> ss = new TreeSet<String>();
ss.add("abc");
ss.add("edf");
ss.forEach(s->System.out.println(s));
for (String string : ss) {
System.out.println(string);
}