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);
}
}
10个Lambda表达式提高效率
于 2023-09-06 19:08:33 首次发布