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<User> list = userMapper.list();
//        list.stream().forEach(user -> System.out.println(user));
        list.stream().forEach(System.out::println);
    }

    @Test
    public void testSort() throws Exception {
        List<String> 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<String> list = Arrays.asList("apple", "banana", "oranges", "pears");
        list.stream().filter(s -> s.startsWith("a")).forEach(System.out::println);
    }

    @Test
    public void testFilter2() {
        List<String> list = Arrays.asList("banana", "oranges", "pears", "apple");
        List<String> 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<String> list = Arrays.asList("apple", "banana", "oranges", "pears");
        list.stream().map(s -> s.length()).forEach(System.out::println);
    }

    @Test
    public void testReduce() throws Exception {
        List<Integer> 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<String> list = Arrays.asList("apple", "banana", "oranges", "pears");
        Map<Integer, List<String>> groups = list.stream().collect(Collectors.groupingBy(String::length));
        System.out.println(groups);
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值