Bootstrap

java8新特性-Lambda表达式和StreamAPI学习

1. Lambda表达式

package com.jsxy.wl;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Predicate;

/**
 * @author Administrator
 * @ClassName LambdaTest1
 * @description: Lambda表达式
 * @date 2024年04月13日
 * @version: 1.0
 */
public class LambdaTest1 {

    //语法格式一:无参,无返回值
    @Test
    public void test1() {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                System.out.println("hello1");
            }
        };
        runnable.run();
        //Lambda表达式
        Runnable runnable1 = () -> System.out.println("hello2");
        runnable1.run();
    }

    //语法格式二:一个参数,无返回值
    @Test
    public void test2() {
        Consumer<String> consumer = new Consumer<String>() {
            @Override
            public void accept(String s) {
                System.out.println(s);
            }
        };
        consumer.accept("hello1");
        //Lambda表达式,一个参数的时候可以省略参数的括号
//        Consumer<String> consumer1 = (s) -> System.out.println(s);
        Consumer<String> consumer1 = s -> System.out.println(s);
        consumer1.accept("hello2");
    }

    //语法格式三:两个参数或以上,可以有返回值
    @Test
    public void test3() {
        Comparator<Integer> comparator = new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                System.out.println(o1);
                System.out.println(o2);
                return o1.compareTo(o2);
            }
        };
        int compare = comparator.compare(1, 2);
        System.out.println(compare);

        //Lambda表达式
        Comparator<Integer> comparator1 = (o1, o2) -> {
            System.out.println(o1);
            System.out.println(o2);
            return o1.compareTo(o2);
        };
        int compare1 = comparator1.compare(2, 1);
        System.out.println(compare1);
    }

    //函数式接口

    /**
     * @FunctionalInterface
     * public interface MyInterface {
     *     void myAbstractMethod();
     * }
     */
//    @Test
//    public void test4(){
//        MyInterface myInterface = () -> System.out.println("helloworld!");
//        myInterface.myAbstractMethod();
//    }

    @Test
    public void test5(){
        List<String> list = Arrays.asList("北京","南京","东京","西京","天津");
        List<String> list1 = filterString(list, new Predicate<String>() {
            @Override
            public boolean test(String s) {
                return s.contains("京");
            }
        });
        System.out.println(list1);

        //lambda表达式改造
        List<String> list2 = filterString(list, s -> s.contains("京"));
        System.out.println(list2);
    }
    //根据给定的规则,过滤集合中的字符串,此规则有Predicate的方法决定
    public List<String> filterString(List<String> list, Predicate<String> predicate){
        ArrayList<String> filterList = new ArrayList<>();
        for (String s: list ) {
            if(predicate.test(s)){
                filterList.add(s);
            }
        }
        return filterList;
    }

}

2. StreamAPI

package com.jsxy.wl;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.IntStream;
import java.util.stream.Stream;

/**
 * @author Administrator
 * @ClassName StreamTest
 * @description: StreamAPI学习
 * @date 2024年04月13日
 * @version: 1.0
 */

/**
 * Stream关注的是计算与CPU有关
 * 集合关注的是数据存储
 */
public class StreamTest {

    //Stream创建方式一:通过集合创建
    @Test
    public void test1(){
        List<String> list = new ArrayList<>();
        list.add("A");
        list.add("B");
        list.add("C");
        list.add("D");
        Stream<String> stream = list.stream();//顺序流
        Stream<String> parallelStream = list.parallelStream();//并行流
    }
    //Stream创建方式一:通过数组创建
    @Test
    public void test2(){
        int[] ints = new int[]{1,2,3,4,5};
        IntStream stream = Arrays.stream(ints);

        User user1 = new User("wang",29);
        User user2 = new User("li",28);
        User[] users = new User[]{user1,user2};
        Stream<User> stream1 = Arrays.stream(users);
    }
    //Stream创建方式三:通过Stream的of()创建
    @Test
    public void test3(){
        Stream<Integer> integerStream = Stream.of(1, 2, 3, 4, 5);
    }

    //Stream中间操作
    @Test
    public void test4(){
        User user1 = new User("wang",29);
        User user2 = new User("li",28);
        List<User> userList = new ArrayList<>();
        userList.add(user1);
        userList.add(user2);
        Stream<User> stream = userList.stream();
        //filter(Predicate p)排除一些元素
        stream.filter(user -> user.getName().equals("li")).forEach(System.out::println);

        //limit(n)截断流 限制元素数量
        userList.stream().limit(1).forEach(System.out::println);

        //skip(n)跳过元素,返回一个扔掉的前n个的流,若不足则返回一个空流
        userList.stream().skip(1).forEach(System.out::println);

        //map映射,获取名字长度大于2的
        userList.stream().map(User::getName).filter(name -> name.length() > 3).forEach(System.out::println);

        //sorted()自然排序
        List<Integer> list = Arrays.asList(34, 23, 56, 6, 98, 37);
        list.stream().sorted().forEach(System.out::println);

        //sorted(Comparator com)定制排序
        userList.stream().sorted((u1,u2) ->
            Integer.compare(u1.getAge(), u2.getAge())
        ).forEach(System.out::println);
        
    }


}


悦读

道可道,非常道;名可名,非常名。 无名,天地之始,有名,万物之母。 故常无欲,以观其妙,常有欲,以观其徼。 此两者,同出而异名,同谓之玄,玄之又玄,众妙之门。

;