- Lambda(Lambda 是希腊字母 λ 的英文名称)表达式本质上是一个匿名方法.也可以称之为闭包。该方法允许把函数作为一个方法的参数(函数作为参数传递进入方法中)。使用该表达式可以使JAVA代码变得更加简洁。
- Lambda表达式的语法
(parameters) -> expression 或 (parameters) ->{ statements; }
- 特征
可选类型声明:不需要声明参数类型,编译器可以统一识别参数值。
可选的参数圆括号:一个参数无需定义圆括号,但多个参数需要定义圆括号。
可选的大括号:如果主体包含了一个语句,不需要使用大括号。
可选的返回关键字:如果主体只有一个表达式返回值则编译器会自动返回值,大括号需要指明表达式返回了一个数值。
4.Lambda表达式的几种格式()->5
:不需要参数,返回值为5x->2*x
:需要参数,返回值为输入值的2倍。(x,y)->x-y
:接受2个参数,并且返回差值。(int x,int y) ->x+y
:接受两个int型的整数,返回他们的和。(String s)->System.out.println(s)
:接受一个String对象,并在控制台打印,不返回任何值。
因此可以总结出Lambda的表达式的格式是:
(params,params) -> {your Code}
params:参数;result:返回值;your Code你自己的代码
5.Lambda表达式示例- 替换静态内部类:
JAVA7的时候我们使用多线程使用静态内部类如下:
public static void OldRunnable(){
new Thread(new Runnable() {
@Override
public void run() {
System.out.println("old runnable !");
}
}).start();
}
这里的代码就会比较多,不是很简洁,那么如果我们使用Lambda的语法格式重写。如下:
public static void newRunnable(){
new Thread(()->System.out.println( "New Runnable , name is "+Thread.currentThread().getName())).start();
}
使用Lambda表达式只需要一行代码就能完成线程的创建。
2. 更加简洁的遍历集合:
/**
* 遍历集合
*/
public static void iteratorTest(){
HashMap<String,String> map = new HashMap<>();
map.put("1","11");
map.put("2","22");
map.put("3","33");
map.put("4","44");
map.forEach((k,v)-> System.out.println("key:"+k+"||V:"+v));
System.out.println("-------------分割线----------------");
Hashtable hashtable = new Hashtable();
hashtable.put("x","XXX");
hashtable.put("y","YYY");
hashtable.put("z","ZZZ");
hashtable.forEach((k,v)-> System.out.println("key>>>>"+k+"||value>>>>>>>"+v));
System.out.println("-------------分割线----------------");
ArrayList list = new ArrayList();
list.add("张三");
list.add("李四");
list.add("王五");
list.forEach(i-> System.out.println("list>>>>>"+i));
}
- 结合map使用,这里的map不是数据集合上的map,这里的map是一个方法,将一个对象变换为另一个对象。
/**
* map 这里map的作用是将一个对象变换为另外一个
*/
public static void mapTest(){
List<Double> cost = Arrays.asList(10.0, 20.0,30.0);
cost.stream().map(x->x*0.5).forEach(y-> System.out.println("cost>>>>"+y));
}
/**
* map转换并且输出线程名
*/
public static void ThreadTest(){
IntStream
.range(0,5)
.boxed()
.map(
i->new Thread(()->System.out.println(Thread.currentThread().getName()))
)
.forEach(Thread::start);
}
- 结合reduce使用,更加便捷的对集合中的数据做求和操作;reduce实现的则是将所有值合并为一个
public static void mapReduceTest(){
List<Double> cost = Arrays.asList(10.0, 20.0,30.0);
double allCost = cost.stream().reduce((sum,x)->sum+x).get();
System.out.println("allCost>>>>"+allCost);
System.out.println("-------------分割线----------------");
HashMap<String,Integer> map = new HashMap<>();
map.put("1",11);
map.put("2",22);
map.put("3",33);
map.put("4",44);
int allCostFormap= map.values().stream().reduce((sum,k)->sum+k).get();
System.out.println("allCostFormap>>>"+allCostFormap);
}
5.结合filter方法对结合进行删选 ,过滤掉一部分不需要的元素
public static void filterTest(){
List<Double> cost = Arrays.asList(10.0, 20.0,30.0);
List<Double> filterCost = cost.stream().filter(x->x>15.0).collect(Collectors.toList());
filterCost.forEach(x-> System.out.println(">>>>>"+x));
System.out.println("-------------分割线----------------");
HashMap<String,Integer> mapBefore = new HashMap<>();
mapBefore.put("1",11);
mapBefore.put("2",22);
mapBefore.put("3",33);
mapBefore.put("4",44);
Map<String,Integer> filterMap = mapBefore
.entrySet()
.stream()
.filter(map ->map.getValue() != 11)
.collect(Collectors.toMap( p -> p.getKey(), p -> p.getValue()));
System.out.println(">>>>>"+filterMap);
}
- 与函数式接口Predicate配合
public static void predicateTest(){
List<String> languages = Arrays.asList("Java","Python","scala","Shell","R");
System.out.println("Start With s :");
filterTest(languages,x->x.startsWith("s"));
}
public static void filterTest(List<String> languages, Predicate<String> condition){
languages.stream().filter(x->condition.test(x)).forEach(x-> System.out.println(">>>>>"+x));
}