java-8 lambda 应用场景:
代码准备:
public static void main(String []args){
List<String> list = new ArrayList<>();
list.add("1");
list.add("4");
list.add("3");
list.add("2");
Map map = new HashMap();
map.put("a","1");
map.put("b","2");
}
代码例子:
//list 循环:
list.forEach(str ->{
System.out.println("str:" + str);
});
//list 删除元素:
list.removeIf(li->"2".equals(li));
//list 排序:升序
list.sort((x,y)->Long.compare(Long.parseLong(x),Long.parseLong(y)));//[1, 2, 3, 4]
//list 排序: 降序
list.sort((x,y)->Long.compare(Long.parseLong(y),Long.parseLong(x)));//[4, 3, 2, 1]
//map 循环
map.forEach((k,v)->{
System.out.println("k:" + k + ",v:" + v);
});