10个Lambda表达式提高效率

package com.mlamp;

import com.mlamp.mapper.UserMapper;
import com.mlamp.pojo.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.*;
import java.util.stream.Collectors;

interface MyInterface {
    public void dosomething(String s);
}

@SpringBootTest
class MybatisQuickstartApplicationTests {
    @Test
    public void testOptions() {
        String str = "hello";
        if (str != null) {
            System.out.println(str.toUpperCase());
        }

        Optional.ofNullable(str).ifPresent(s -> System.out.println(s.toUpperCase()));
        Optional.ofNullable(str).map(String::toUpperCase).ifPresent(System.out::println);
    }
    @Test
    public void testThread() {
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("thread");
            }
        });
        thread.start();

        Thread thread1 = new Thread(() -> System.out.println("lambda"));
        thread1.start();
    }

    @Test
    public void testInterface() {
        MyInterface interface1 = new MyInterface() {
            @Override
            public void dosomething(String s) {
                System.out.println(s);
            }
        };
        interface1.dosomething("hello1");

        MyInterface interface2 = (s) -> System.out.println(s);
        interface2.dosomething("hello2");
    }

    @Autowired
    private UserMapper userMapper;
    @Test
    public void testListUser() {
        List list = userMapper.list();
//        list.stream().forEach(user -> System.out.println(user));
        list.stream().forEach(System.out::println);
    }

    @Test
    public void testSort() throws Exception {
        List list = Arrays.asList("c", "b", "a");
        Collections.sort(list,  (s1, s2) -> s1.compareTo(s2));
        System.out.println(list);
    }

    @Test
    public void testFilter() throws Exception {
        List list = Arrays.asList("apple", "banana", "oranges", "pears");
        list.stream().filter(s -> s.startsWith("a")).forEach(System.out::println);
    }

    @Test
    public void testFilter2() {
        List list = Arrays.asList("banana", "oranges", "pears", "apple");
        List list1 = list.stream().filter(s -> s.contains("a")).map(String::toUpperCase).sorted().collect(Collectors.toList());
        System.out.println(list1);
    }

    @Test
    public void testLen() throws Exception {
        List list = Arrays.asList("apple", "banana", "oranges", "pears");
        list.stream().map(s -> s.length()).forEach(System.out::println);
    }

    @Test
    public void testReduce() throws Exception {
        List list = Arrays.asList(1,2,3);
        int sum = list.stream().reduce(0, (a, b) -> a + b);
        System.out.println(sum);
    }

    @Test
    public void testGroups() throws Exception {
        List list = Arrays.asList("apple", "banana", "oranges", "pears");
        Map> groups = list.stream().collect(Collectors.groupingBy(String::length));
        System.out.println(groups);
    }
}

你可能感兴趣的:(java)