Java8 lamada表达式的简单使用

来源:互联网 发布:filter 过滤css和js 编辑:程序博客网 时间:2024/06/08 16:07

首先是配置。。。
在project的build.gradle中添加

classpath 'me.tatarka:gradle-retrolambda:3.2.0'

这里写图片描述

在module的build.gradle中添加

 //设置JDK1.8    compileOptions {        sourceCompatibility JavaVersion.VERSION_1_8        targetCompatibility JavaVersion.VERSION_1_8    }    jackOptions {            enabled true        }

位置如下:

这里写图片描述

哈哈哈,接下来就开始我们的lamda之旅啦。。。

MainActivity.class

public class MainActivity extends AppCompatActivity {    private static final String TAG = "cwy";    /**     * ATTENTION: This was auto-generated to implement the App Indexing API.     * See https://g.co/AppIndexing/AndroidStudio for more information.     */    private GoogleApiClient client;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        findViewById(R.id.button1).setOnClickListener(view -> onButton1());        findViewById(R.id.button2).setOnClickListener(view -> onButton2());        findViewById(R.id.button3).setOnClickListener(view -> onButton3());        findViewById(R.id.button4).setOnClickListener(view -> onButton4());        findViewById(R.id.button5).setOnClickListener(view -> onButton5());        findViewById(R.id.button6).setOnClickListener(view -> onButton6());        // ATTENTION: This was auto-generated to implement the App Indexing API.        // See https://g.co/AppIndexing/AndroidStudio for more information.        client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();    }    public void onButton1() {        Runnable runnable = () -> Toast.makeText(this, "My name is cwy", Toast.LENGTH_SHORT).show();        runnable.run();    }    /*    * 平方数    * */    public void onButton2() {        List<Integer> nums = Arrays.asList(1, 2, 3, 4);        List<Integer> squareNums =                nums.stream()                        .map(n -> n * n)                        .collect(Collectors.toList());        squareNums.forEach(p -> System.out.println(p));    }    /*    * 转换大写    *    * */    public void onButton3() {        List<String> wordList = Arrays.asList("a", "b", "c");        List<String> output =                wordList.stream()                        .map(String::toUpperCase)                        .collect(Collectors.toList());        output.forEach(p -> System.out.println(p));    }    /*    *一对多的关系    *    * */    public void onButton4() {        Stream<List<Integer>> inputStream = Stream.of(                Arrays.asList(1),                Arrays.asList(2, 3),                Arrays.asList(4, 5, 6)        );        Stream<Integer> outputStream = inputStream.flatMap((childList) -> childList.stream());        outputStream.forEach(p -> System.out.println(p));    }    /*    *    *    * */    public void onButton5() {        // 筛选偶数        Integer[] sixNums = {1, 2, 3, 4, 5, 6};        Integer[] evens = Stream.of(sixNums)                .filter(n -> n%2 == 0)                .toArray(Integer[]::new);        Stream<Integer> stream = Arrays.stream(evens);        stream.forEach(p -> System.out.println(p));    }    /*    *    *    * */    public void onButton6() {        // 字符串连接,concat = "ABCD"        String concat = Stream.of("A", "B", "C", "D")                .reduce("", String::concat);        System.out.println(concat);        // 求最小值,minValue = -3.0        double minValue = Stream.of(-1.5, 1.0, -3.0, -2.0)                .reduce(Double.MAX_VALUE, Double::min);        System.out.println(minValue);        // 求和,sumValue = 10, 有起始值        int sumValue = Stream.of(1, 2, 3, 4)                .reduce(0, Integer::sum);        System.out.println(sumValue);        // 过滤,字符串连接,concat = "ace"        concat = Stream.of("a", "B", "c", "D", "e", "F")                . filter(x -> x.compareTo("Z") > 0)                . reduce("", String::concat);        System.out.println(concat);    }}

activity_main.xml中只有六个Button按钮

<Button        android:text="Button1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentTop="true"        android:layout_alignParentLeft="true"        android:layout_alignParentStart="true"        android:id="@+id/button1" />    <Button        android:text="Button2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentTop="true"        android:layout_alignParentRight="true"        android:layout_alignParentEnd="true"        android:id="@+id/button2" />    <Button        android:text="Button3"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_below="@+id/button1"        android:layout_alignRight="@+id/button1"        android:layout_alignEnd="@+id/button1"        android:layout_marginTop="49dp"        android:id="@+id/button3" />    <Button        android:text="Button4"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignTop="@+id/button3"        android:layout_alignLeft="@+id/button2"        android:layout_alignStart="@+id/button2"        android:id="@+id/button4" />    <Button        android:text="Button5"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerVertical="true"        android:layout_alignRight="@+id/button3"        android:layout_alignEnd="@+id/button3"        android:id="@+id/button5" />    <Button        android:text="Button6"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignTop="@+id/button5"        android:layout_alignParentRight="true"        android:layout_alignParentEnd="true"        android:id="@+id/button6" />
原创粉丝点击