Home > Java > javaTutorial > body text

Quick Start with Lambda Expressions in Java

高洛峰
Release: 2017-01-23 13:40:00
Original
1487 people have browsed it

Lambda Introduction

Lambda expressions are an important new feature in Java SE 8. Lambda expressions allow you to replace functional interfaces with expressions. A lambda expression is just like a method, it provides a normal parameter list and a body (body, which can be an expression or a code block) that uses these parameters.

Lambda expressions also enhance the collection library. Java SE 8 adds two packages for batch operations on collection data: the java.util.function package and the java.util.stream package. A stream is just like an iterator, but with many additional features. Overall, lambda expressions and streams are the biggest changes to the Java language since the addition of generics and annotations.

Lambda expression is essentially an anonymous method, and its bottom layer is implemented through the invokedynamic instruction to generate an anonymous class. It provides a simpler syntax and writing style, allowing you to replace functional interfaces with expressions. In the opinion of some people, Lambda can make your code more concise and can be avoided at all. This view is of course fine, but the important thing is that lambda brings closure to Java. Thanks to Lamdba's support for collections, the performance of collection traversal through Lambda is greatly improved under multi-core processor conditions. In addition, we can process collections in a data flow manner - which is very attractive.

Lambda syntax

The syntax of Lambda is extremely simple, similar to the following structure:

(parameters) -> expression
Copy after login

or

(parameters) -> { statements; }
Copy after login

Lambda expression consists of three parts:

1. Paramaters: Similar to the formal parameter list in a method, the parameters here are parameters in the functional interface. The parameter types here can be explicitly declared or not declared and implicitly inferred by the JVM. In addition, the parentheses can be omitted when there is only one inferred type.

2. ->: Can be understood as meaning "to be used for"

3. Method body: It can be an expression or a code block, or a function The implementation of the methods in the interface. A code block can return a value or nothing. The code block here is equivalent to the method body of the method. If it is an expression, it can also return a value or nothing.

We use the following examples to illustrate:

//示例1:不需要接受参数,直接返回10
()->10
 
//示例2:接受两个int类型的参数,并返回这两个参数相加的和
(int x,int y)->x+y;
 
//示例2:接受x,y两个参数,该参数的类型由JVM根据上下文推断出来,并返回两个参数的和
(x,y)->x+y;
 
//示例3:接受一个字符串,并将该字符串打印到控制到,不反回结果
(String name)->System.out.println(name);
 
//示例4:接受一个推断类型的参数name,并将该字符串打印到控制台
name->System.out.println(name);
 
//示例5:接受两个String类型参数,并分别输出,不反回
(String name,String sex)->{System.out.println(name);System.out.println(sex)}
 
//示例6:接受一个参数x,并返回该该参数的两倍
x->2*x
Copy after login

Where is Lambda used?

In [ Functional interface][1] We know that the target type of a Lambda expression is a functional interface - each Lambda can be matched with a given type through a specific functional interface. Therefore, a lambda expression can be used anywhere that matches its target type. The lambda expression must have the same parameter types as the abstract function description of the functional interface. Its return type must also be compatible with the return type of the abstract function, and it must be The exceptions that can be thrown are also limited to the description scope of the function.

Next, let’s look at an example of a custom functional interface:

@FunctionalInterface
 interface Converter<F, T>{
 
   T convert(F from);
 
}
Copy after login

First use the interface in the traditional way:

Converter<String ,Integer> converter=new Converter<String, Integer>() {
     @Override
     public Integer convert(String from) {
       return Integer.valueOf(from);
     }
   };
 
   Integer result = converter.convert("200");
   System.out.println(result);
Copy after login

Obviously there is no problem with this, then the next step is when Lambda comes on stage. Use Lambda to implement the Converter interface:

Converter<String ,Integer> converter=(param) -> Integer.valueOf(param);
    Integer result = converter.convert("101");
    System.out.println(result);
Copy after login

Through the above example, I think you have already We have a simple understanding of the use of Lambda. Below, we are using a commonly used Runnable to demonstrate:

In the past we might have written this kind of code:

new Thread(new Runnable() {
      @Override
      public void run() {
        System.out.println("hello lambda");
      }
    }).start();
Copy after login

In some cases, a large number of anonymous classes will make the code appear cluttered. Now you can use Lambda to make it concise:

new Thread(() -> System.out.println("hello lambda")).start();
Copy after login

Method reference

Method reference is a simplified way of writing Lambda expression. The referenced method is actually the implementation of the method body of the Lambda expression. Its syntax structure is:

ObjectRef::methodName
Copy after login

The left side can be the class name or instance name, and the middle is the method reference symbol":: ", the right side is the corresponding method name.

Method references are divided into three categories:

1. Static method references

In some cases, we may write code like this:

public class ReferenceTest {
  public static void main(String[] args) {
    Converter<String ,Integer> converter=new Converter<String, Integer>() {
      @Override
      public Integer convert(String from) {
        return ReferenceTest.String2Int(from);
      }
    };
    converter.convert("120");
 
  }
 
  @FunctionalInterface
  interface Converter<F,T>{
    T convert(F from);
  }
 
  static int String2Int(String from) {
    return Integer.valueOf(from);
  }
}
Copy after login

If you use static references at this time, the code will be more concise:

Converter<String, Integer> converter = ReferenceTest::String2Int;
converter.convert("120");
Copy after login

##2. Instance method Quoting

We may also write code like this:

public class ReferenceTest {
  public static void main(String[] args) {
 
    Converter<String, Integer> converter = new Converter<String, Integer>() {
      @Override
      public Integer convert(String from) {
        return new Helper().String2Int(from);
      }
    };
    converter.convert("120");
  }
 
  @FunctionalInterface
  interface Converter<F, T> {
    T convert(F from);
  }
 
  static class Helper {
    public int String2Int(String from) {
      return Integer.valueOf(from);
    }
  }
}
Copy after login

The same reference using instance methods will be more concise:

Helper helper = new Helper();
Converter<String, Integer> converter = helper::String2Int;
converter.convert("120");
Copy after login

3. Constructor reference

Now we will demonstrate the constructor reference. First we define a parent class Animal:

class Animal{
  private String name;
  private int age;
 
  public Animal(String name, int age) {
    this.name = name;
    this.age = age;
  }
 
  public void behavior(){
 
  }
}
Copy after login

Next, we define two subclasses of Animal: Dog, Bird

public class Bird extends Animal {
 
  public Bird(String name, int age) {
    super(name, age);
  }
 
  @Override
  public void behavior() {
    System.out.println("fly");
  }
}
 
class Dog extends Animal {
 
  public Dog(String name, int age) {
    super(name, age);
  }
 
  @Override
  public void behavior() {
    System.out.println("run");
  }
}
Copy after login

Then we define the factory interface:

interface Factory<T extends Animal> {
  T create(String name, int age);
}
Copy after login


Next we still use the traditional method to create the Dog class and Bird class Object:

Factory factory=new Factory() {
  @Override
  public Animal create(String name, int age) {
    return new Dog(name,age);
  }
};
factory.create("alias", 3);
factory=new Factory() {
  @Override
  public Animal create(String name, int age) {
    return new Bird(name,age);
  }
};
factory.create("smook", 2);
Copy after login

I wrote more than ten codes just to create two objects. Now let’s try using constructor reference:

Factory<Animal> dogFactory =Dog::new;
Animal dog = dogFactory.create("alias", 4);
 
Factory<Bird> birdFactory = Bird::new;
Bird bird = birdFactory.create("smook", 3);
Copy after login

This way the code will look clean and neat. When constructing an object through Dog::new, the signature of the Factory.create function selects the corresponding constructor.

Lambda’s domain and access restrictions

域即作用域,Lambda表达式中的参数列表中的参数在该Lambda表达式范围内(域)有效。在作用Lambda表达式内,可以访问外部的变量:局部变量、类变量和静态变量,但操作受限程度不一。

访问局部变量

在Lambda表达式外部的局部变量会被JVM隐式的编译成final类型,因此只能访问外而不能修改。

public class ReferenceTest {
  public static void main(String[] args) {
 
    int n = 3;
    Calculate calculate = param -> {
      //n=10; 编译错误
      return n + param;
    };
    calculate.calculate(10);
  }
 
  @FunctionalInterface
  interface Calculate {
    int calculate(int value);
  }
 
}
Copy after login

访问静态变量和成员变量

在Lambda表达式内部,对静态变量和成员变量可读可写。

public class ReferenceTest {
  public int count = 1;
  public static int num = 2;
 
  public void test() {
    Calculate calculate = param -> {
      num = 10;//修改静态变量
      count = 3;//修改成员变量
      return n + param;
    };
    calculate.calculate(10);
  }
 
  public static void main(String[] args) {
 
  }
 
  @FunctionalInterface
  interface Calculate {
    int calculate(int value);
  }
 
}
Copy after login


Lambda不能访问函数接口的默认方法

java8增强了接口,其中包括接口可添加default关键词定义的默认方法,这里我们需要注意,Lambda表达式内部不支持访问默认方法。

Lambda实践

在[函数式接口][2]一节中,我们提到java.util.function包中内置许多函数式接口,现在将对常用的函数式接口做说明。

Predicate接口

输入一个参数,并返回一个Boolean值,其中内置许多用于逻辑判断的默认方法:

@Test
public void predicateTest() {
  Predicate<String> predicate = (s) -> s.length() > 0;
  boolean test = predicate.test("test");
  System.out.println("字符串长度大于0:" + test);
 
  test = predicate.test("");
  System.out.println("字符串长度大于0:" + test);
 
  test = predicate.negate().test("");
  System.out.println("字符串长度小于0:" + test);
 
  Predicate<Object> pre = Objects::nonNull;
  Object ob = null;
  test = pre.test(ob);
  System.out.println("对象不为空:" + test);
  ob = new Object();
  test = pre.test(ob);
  System.out.println("对象不为空:" + test);
}
Copy after login


Function接口

接收一个参数,返回单一的结果,默认的方法(andThen)可将多个函数串在一起,形成复合Funtion(有输入,有输出)结果,

@Test
public void functionTest() {
  Function<String, Integer> toInteger = Integer::valueOf;
  //toInteger的执行结果作为第二个backToString的输入
  Function<String, String> backToString = toInteger.andThen(String::valueOf);
  String result = backToString.apply("1234");
  System.out.println(result);
 
  Function<Integer, Integer> add = (i) -> {
    System.out.println("frist input:" + i);
    return i * 2;
  };
  Function<Integer, Integer> zero = add.andThen((i) -> {
    System.out.println("second input:" + i);
    return i * 0;
  });
 
  Integer res = zero.apply(8);
  System.out.println(res);
}
Copy after login


Supplier接口

返回一个给定类型的结果,与Function不同的是,Supplier不需要接受参数(供应者,有输出无输入)

@Test
public void supplierTest() {
  Supplier<String> supplier = () -> "special type value";
  String s = supplier.get();
  System.out.println(s);
}
Copy after login

Consumer接口

代表了在单一的输入参数上需要进行的操作。和Function不同的是,Consumer没有返回值(消费者,有输入,无输出)

@Test
public void consumerTest() {
  Consumer<Integer> add5 = (p) -> {
    System.out.println("old value:" + p);
    p = p + 5;
    System.out.println("new value:" + p);
  };
  add5.accept(10);
}
Copy after login

以上四个接口的用法代表了java.util.function包中四种类型,理解这四个函数式接口之后,其他的接口也就容易理解了,现在我们来做一下简单的总结:

Predicate用来逻辑判断,Function用在有输入有输出的地方,Supplier用在无输入,有输出的地方,而Consumer用在有输入,无输出的地方。你大可通过其名称的含义来获知其使用场景。

Stream

Lambda为java8带了闭包,这一特性在集合操作中尤为重要:java8中支持对集合对象的stream进行函数式操作,此外,stream api也被集成进了collection api,允许对集合对象进行批量操作。

下面我们来认识Stream。

Stream表示数据流,它没有数据结构,本身也不存储元素,其操作也不会改变源Stream,而是生成新Stream.作为一种操作数据的接口,它提供了过滤、排序、映射、规约等多种操作方法,这些方法按照返回类型被分为两类:凡是返回Stream类型的方法,称之为中间方法(中间操作),其余的都是完结方法(完结操作)。完结方法返回一个某种类型的值,而中间方法则返回新的Stream。中间方法的调用通常是链式的,该过程会形成一个管道,当完结方法被调用时会导致立即从管道中消费值,这里我们要记住:Stream的操作尽可能以“延迟”的方式运行,也就是我们常说的“懒操作”,这样有助于减少资源占用,提高性能。对于所有的中间操作(除sorted外)都是运行在延迟模式下。

Stream不但提供了强大的数据操作能力,更重要的是Stream既支持串行也支持并行,并行使得Stream在多核处理器上有着更好的性能。

Stream的使用过程有着固定的模式:

1、创建Stream

2、通过中间操作,对原始Stream进行“变化”并生成新的Stream

3、使用完结操作,生成最终结果

也就是

创建——>变化——>完结

Stream的创建

对于集合来说,可以通过调用集合的stream()或者parallelStream()来创建,另外这两个方法也在Collection接口中实现了。对于数组来说,可以通过Stream的静态方法of(T … values)来创建,另外,Arrays也提供了有关stream的支持。

除了以上基于集合或者数组来创建Stream,也可以通过Steam.empty()创建空的Stream,或者利用Stream的generate()来创建无穷的Stream。

下面我们以串行Stream为例,分别说明Stream几种常用的中间方法和完结方法。首先创建一个List集合:

List<String> lists=new ArrayList<String >();
    lists.add("a1");
    lists.add("a2");
    lists.add("b1");
    lists.add("b2");
    lists.add("b3");
    lists.add("o1");
Copy after login

中间方法

过滤器(Filter)

结合Predicate接口,Filter对流对象中的所有元素进行过滤,该操作是一个中间操作,这意味着你可以在操作返回结果的基础上进行其他操作。

public static void streamFilterTest() {
  lists.stream().filter((s -> s.startsWith("a"))).forEach(System.out::println);
 
  //等价于以上操作
  Predicate<String> predicate = (s) -> s.startsWith("a");
  lists.stream().filter(predicate).forEach(System.out::println);
 
  //连续过滤
  Predicate<String> predicate1 = (s -> s.endsWith("1"));
  lists.stream().filter(predicate).filter(predicate1).forEach(System.out::println);
}
Copy after login

排序(Sorted)

结合Comparator接口,该操作返回一个排序过后的流的视图,原始流的顺序不会改变。通过Comparator来指定排序规则,默认是按照自然顺序排序。

public static void streamSortedTest() {
  System.out.println("默认Comparator");
  lists.stream().sorted().filter((s -> s.startsWith("a"))).forEach(System.out::println);
 
  System.out.println("自定义Comparator");
  lists.stream().sorted((p1, p2) -> p2.compareTo(p1)).filter((s -> s.startsWith("a"))).forEach(System.out::println);
 
}
Copy after login

映射(Map)

结合Function接口,该操作能将流对象中的每个元素映射为另一种元素,实现元素类型的转换。

public static void streamMapTest() {
  lists.stream().map(String::toUpperCase).sorted((a, b) -> b.compareTo(a)).forEach(System.out::println);
 
  System.out.println("自定义映射规则");
  Function<String, String> function = (p) -> {
    return p + ".txt";
  };
  lists.stream().map(String::toUpperCase).map(function).sorted((a, b) -> b.compareTo(a)).forEach(System.out::println);
 
}
Copy after login


在上面简单介绍了三种常用的操作,这三种操作极大简化了集合的处理。接下来,介绍几种完结方法:

完结方法

“变换”过程之后,需要获取结果,即完成操作。下面我们来看相关的操作:

匹配(Match)

用来判断某个predicate是否和流对象相匹配,最终返回Boolean类型结果,例如:

public static void streamMatchTest() {
  //流对象中只要有一个元素匹配就返回true
  boolean anyStartWithA = lists.stream().anyMatch((s -> s.startsWith("a")));
  System.out.println(anyStartWithA);
  //流对象中每个元素都匹配就返回true
  boolean allStartWithA
      = lists.stream().allMatch((s -> s.startsWith("a")));
  System.out.println(allStartWithA);
}
Copy after login

收集(Collect)

在对经过变换之后,我们将变换的Stream的元素收集,比如将这些元素存至集合中,此时便可以使用Stream提供的collect方法,例如:

public static void streamCollectTest() {
  List<String> list = lists.stream().filter((p) -> p.startsWith("a")).sorted().collect(Collectors.toList());
  System.out.println(list);
 
}
Copy after login

计数(Count)

类似sql的count,用来统计流中元素的总数,例如:

public static void streamCountTest() {
  long count = lists.stream().filter((s -> s.startsWith("a"))).count();
  System.out.println(count);
}
Copy after login

规约(Reduce)

reduce方法允许我们用自己的方式去计算元素或者将一个Stream中的元素以某种规律关联,例如:

public static void streamReduceTest() {
  Optional<String> optional = lists.stream().sorted().reduce((s1, s2) -> {
    System.out.println(s1 + "|" + s2);
    return s1 + "|" + s2;
  });
}
Copy after login

执行结果如下:

a1|a2
a1|a2|b1
a1|a2|b1|b2
a1|a2|b1|b2|b3
a1|a2|b1|b2|b3|o1
Copy after login

并行Stream VS 串行Stream

到目前我们已经将常用的中间操作和完结操作介绍完了。当然所有的的示例都是基于串行Stream。接下来介绍重点戏——并行Stream(parallel Stream)。并行Stream基于Fork-join并行分解框架实现,将大数据集合切分为多个小数据结合交给不同的线程去处理,这样在多核处理情况下,性能会得到很大的提高。这和MapReduce的设计理念一致:大任务化小,小任务再分配到不同的机器执行。只不过这里的小任务是交给不同的处理器。

通过parallelStream()创建并行Stream。为了验证并行Stream是否真的能提高性能,我们执行以下测试代码:

首先创建一个较大的集合:

List<String> bigLists = new ArrayList<>();
  for (int i = 0; i < 10000000; i++) {
    UUID uuid = UUID.randomUUID();
    bigLists.add(uuid.toString());
  }
Copy after login

测试串行流下排序所用的时间:

private static void notParallelStreamSortedTest(List<String> bigLists) {
  long startTime = System.nanoTime();
  long count = bigLists.stream().sorted().count();
  long endTime = System.nanoTime();
  long millis = TimeUnit.NANOSECONDS.toMillis(endTime - startTime);
  System.out.println(System.out.printf("串行排序: %d ms", millis));
 
}
Copy after login

测试并行流下排序所用的时间:

private static void parallelStreamSortedTest(List<String> bigLists) {
  long startTime = System.nanoTime();
  long count = bigLists.parallelStream().sorted().count();
  long endTime = System.nanoTime();
  long millis = TimeUnit.NANOSECONDS.toMillis(endTime - startTime);
  System.out.println(System.out.printf("并行排序: %d ms", millis));
 
}
Copy after login

结果如下:

串行排序: 13336 ms
并行排序: 6755 ms

看到这里,我们确实发现性能提高了约么50%,你也可能会想以后都用parallel Stream不久行了么?实则不然,如果你现在还是单核处理器,而数据量又不算很大的情况下,串行流仍然是这种不错的选择。你也会发现在某些情况,串行流的性能反而更好,至于具体的使用,需要你根据实际场景先测试后再决定。

懒操作

上面我们谈到Stream尽可能以延迟的方式运行,这里通过创建一个无穷大的Stream来说明:

首先通过Stream的generate方法来一个自然数序列,然后通过map变换Stream:

//递增序列
class NatureSeq implements Supplier<Long> {
   long value = 0;
 
   @Override
   public Long get() {
     value++;
     return value;
   }
 }
 
public void streamCreateTest() {
   Stream<Long> stream = Stream.generate(new NatureSeq());
   System.out.println("元素个数:"+stream.map((param) -> {
     return param;
   }).limit(1000).count());
 
 }
Copy after login

   

执行结果为:

       元素个数:1000

我们发现开始时对这个无穷大的Stream做任何中间操作(如:filter,map等,但sorted不行)都是可以的,也就是对Stream进行中间操作并生存一个新的Stream的过程并非立刻生效的(不然此例中的map操作会永远的运行下去,被阻塞住),当遇到完结方法时stream才开始计算。通过limit()方法,把这个无穷的Stream转为有穷的Stream。

总结

以上就是Java Lambda快速入门详解的全部内容,看完本文后大家是不是对Java Lambda有了更深的了解,希望本文对大家学习Java Lambda能有所帮助。

更多快速入门Java中的Lambda表达式相关文章请关注PHP中文网!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template