//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);
}