Java8 list stream 测试用例小结

一、测试对象

package com.demo.crwien.test;

public class Student {
    private Integer id;
    private String groupId;
    private String name;
    private Integer age;
}

二、测试用例

package com.demo.lee.util;

import org.junit.Test;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.groupingBy;

/**
 * @author lee
 */
public class ListCollectionTest {

    @Test
    public void test1() {
        //1.分组计数
        List<Student> list = Arrays.asList(
                new Student(1, "one", "zhao",11),
                new Student(2, "one", "qian",22),
                new Student(3, "two", "sun",33),
                new Student(4, "two", "lee",18));


        //1.1根据某个属性分组计数
        Map<String, Long> result1 = list.stream().collect(Collectors.groupingBy(Student::getGroupId, Collectors.counting()));
        System.out.println(result1);

        //1.2根据整个实体对象分组计数,当其为String时常使用
        Map<Student, Long> result2 = list.stream().collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
        System.out.println(result2);

        //1.3根据分组的key值对结果进行排序、放进另一个map中并输出
        Map<String, Long> xMap = new HashMap<>();
        result1.entrySet().stream().sorted(Map.Entry.<String, Long>comparingByKey().reversed()) //reversed不生效
                .forEachOrdered(x -> xMap.put(x.getKey(), x.getValue()));
        System.out.println(xMap);

    }

    @Test
    public void test2() {
        List<Student> list = Arrays.asList(
                new Student(1, "one", "zhao",11),
                new Student(2, "one", "qian",22),
                new Student(3, "two", "sun",33),
                new Student(4, "two", "lee",18));


        //2.分组,并统计其中一个属性值得sum或者avg:id总和
        Map<String, Integer> result3 = list.stream().collect(
                Collectors.groupingBy(Student::getGroupId, Collectors.summingInt(Student::getId))
        );
        System.out.println(result3);
    }

    @Test
    public void test3(){
        List<Student> list = Arrays.asList(
                new Student(1, "one", "zhao",11),
                new Student(2, "one", "qian",22),
                new Student(3, "two", "sun",33),
                new Student(4, "two", "lee",18));

        //3.根据某个属性过滤
        List<Student> list1 = list.stream().filter(a -> a.getName().contains("a")).collect(Collectors.toList());
        System.out.println(list1);
    }

    @Test
    public void test4(){
        List<Student> list = Arrays.asList(
                new Student(1, "one", "zhao",11),
                new Student(2, "one", "qian",22),
                new Student(3, "two", "sun",33),
                new Student(4, "two", "lee",18));

        //4.根据对象属性进行分组,自行编辑分组条件
        Map<String, List<Student>> collect = list.stream().collect(groupingBy(s -> {
            Integer age = s.getAge();
            if (0<age && age<=10){
                return "baby";
            }else if (10<age && age<=20){
                return "teenager";
            }else if (20<age && age<=30){
                return "youth";
            }else {
                return "old";
            }
        }));

        System.out.println(collect);
    }

@test
public void test5(){
    List<Student> list = Arrays.asList(
                new Student(1, "one", "zhao",11),
                new Student(2, "one", "qian",22),
                new Student(3, "two", "sun",33),
                new Student(4, "two", "lee",18));
                //对象属性拼字符串
     String investor = list.stream().map(Student::getName).collect(Collectors.joining(",")
}

@test
public void test6(){
    List<Student> list = Arrays.asList(
                new Student(1, "one", "zhao",11),
                new Student(2, "one", "qian",22),
                new Student(3, "two", "sun",33),
                new Student(4, "two", "lee",18));
                //对象属性求和
     Integer result = list.stream().collect(Collectors.summingInt(Student::getAge));
     System.out.println("所有学生年龄之和 : " + reuslt);
}

@test
public void test7(){
    List<Student> list = Arrays.asList(
                new Student(1, "one", "zhao",11),
                new Student(2, "one", "qian",22),
                new Student(3, "two", "sun",33),
                new Student(4, "two", "lee",18));
 //list转map
        Map<Integer,String> map = userlist.stream().collect(Collectors.toMap(User::getAge,User::getName));
}

@test
public void test8(){
    //按名称描述排序
    List<ECLabelEnum> enumList = Arrays.stream(ECLabelEnum.values()).sorted(Comparator.comparing(ECLabelEnum::desc)).collect(Collectors.toList());
}

//排序
@test
public void test9(){
    //倒序,从大到小
    list.stream().sorted(Comparator.comparing(Student::getAge));
    //正序,从小到大
    list.stream().sorted(Comparator.comparing(Student::getAge).reversed()) 
}
}

三、实战

Map<String, String> dataMap = new HashMap<>(10);
List<String> dataList = request.getDataList();

// 获取未加密的字符串(过滤不符合条件的)
List<String> unEncryptData = dataList.stream().filter(s -> !isEncryptData(s)).collect(Collectors.toList());

// 移除未加密的
dataList.removeAll(unEncryptData);

if (encryptData != null && encryptData.size() > 0) {
      collect = encryptData.stream().collect(Collectors.toMap(x -> x, x -> x,(k1,k2) -> k1));
    }
阅读 155

推荐阅读