Introduction to Lambda expressions-the creation and common methods of Stream

One, Stream streaming:

  1. Stream streaming is a multi-data processing technology based on lambda
  2. Stream highly abstracts collection data processing, greatly simplifying the amount of code

  3. Insert picture description here
    Stream can perform a series of processing on the collection, such as de-
    duplication , filtering, sorting, aggregation, etc. Two, five ways to create Stream PS: the first two are the most commonly used, the latter can be understood
 // 基于数组进行创建
    @Test
    public void generator1() {
    
    
        String[] str = {
    
    "lucy", "lily", "jack"};
        Stream<String> stream = Stream.of(str);
        stream.forEach(s -> System.out.println(s));
    }
 // 基于集合进行创建
    @Test
    public void generator2() {
    
    
        List<String> list = new ArrayList<>();
        list.add("lucy");
        list.add("Lily");
        list.add("jack");
        Stream<String> stream = list.stream();
        stream.forEach(s -> System.out.println(s));
    }

 // 利用generator方法创建无线长度流
    @Test
    public void generator3() {
    
    
        Stream<Integer> stream = Stream.generate(() -> new Random().nextInt(1000));
        // 持续进行输出
//        stream.forEach(i -> System.out.println(i));
        // 加上limit做限制以后,输出个数为limit参数设置的长度
        stream.limit(10).forEach(i -> System.out.println(i));
    }

 //基于迭代器创建流
    @Test
    public void generator4() {
    
    
        Stream<Integer> stream = Stream.iterate(1, n -> n + 1);
        // 持续进行输出
//        stream.forEach(i -> System.out.println(i));
        // 加上limit做限制以后,输出个数为limit参数设置的长度
        stream.limit(10).forEach(i -> System.out.println(i));
    }
 // 基于字符序列创建流
    @Test
    public void generator5() {
    
    
        String str = "abcdefg";
        IntStream stream = str.chars();
        // 输出的整数是字符的ASCII码,如果需要转换成中文,可以加上charAt
//        stream.forEach(i -> System.out.println(i));
        stream.forEach(i -> System.out.println((char) i));
    }

Three, commonly used methods:

interface use
foreach Loop traversal
map The map method is used to map each element to the corresponding result
filter The filter method is used to filter out elements through the set conditions
limit The limit method is used to obtain a specified number of streams
sorted The sorted method is used to sort the stream
Collectors The Collectors class implements the conversion of streams into collections and aggregate elements
// 提取集合中所有偶数的和
    @Test
    public void demo1() {
    
    
        List<String> list = Arrays.asList("1", "2", "3", "4", "5", "6");
        int sum = list.stream()
                .mapToInt(n -> Integer.parseInt(n))//转为整型数组
                .filter(n -> n % 2 == 0)  // 过滤器,过滤到无用的数字
                .sum();  // 求和
        System.out.println(sum);
    }

 // 所有名字首字母大写
    @Test
    public void demo2() {
    
    
        List<String> list = Arrays.asList("lucy", "lily", "jack", "lucy");
        List newList = list.stream()
                .map(s -> s.substring(0, 1).toUpperCase() + s.substring(1)) //按规则对每一个流数据进行转换
                .collect(Collectors.toList());// collect对数据进行收集,转为list数组,如需过滤重复,可以用set方法,如下
        System.out.println(newList);
    }
  
 @Test
    public void demo3() {
    
    
        List<String> list = Arrays.asList("lucy", "lily", "jack", "lucy");
        Set newSet = list.stream()
                .map(s -> s.substring(0, 1).toUpperCase() + s.substring(1))
                .collect(Collectors.toSet());
        System.out.println(newSet);
    }
 // 将所有奇数进行从大到小的排序,且不允许重复
    @Test
    public void demo4() {
    
    
        List<Integer> list = Arrays.asList(100, 31, 35, 13, 31, 100, 23);
        List newList = list.stream()
                .distinct()  //去重
                .filter(s -> s % 2 == 1) // 求奇数
                .sorted((a, b) -> b - a)  // 从大到小进行排序,A代表前一个数据,b代表后一个数据,b-a表示数据从大到小进行排列
                .collect(Collectors.toList()); // 转换成集合
        System.out.println(newList);
    }

Guess you like

Origin blog.csdn.net/qq_36792120/article/details/112099790