jdk8 stream常用list转map方式

457 阅读1分钟

list对象转map对象

List<User> userList = new ArrayList<>();
Map<String, User> map = userList.stream().collect(Collectors.toMap(User::getName, Function.identity(), (key1, key2) -> key2));

list对象转list string

List<Dict> list = new ArrayList<>();
list.stream().map(DictDTO::getType).collect(Collectors.toList())

list对象转map list

List<Dict> list = new ArrayList<>();
Map<String, List<Dict>> map = list.stream().collect(Collectors.groupingBy(Dict::getType));

list对象转map字符串

Map<String,String> dictMap = list.stream().collect(Collectors.toMap(d->{return d.getType()+"_"+d.getCode();}, Dict::getName, (key1, key2) -> key2))

list对象转map<type,map<k,v>>

List<Dict> list = new ArrayList<Dict>();
Map<String, Map<String, String>> typeMap = list.stream()
        .collect(Collectors.groupingBy(
            Dict::getType,
            Collectors.toMap(
                Dict::getItemValue,
                Dict::getItemName,
                (oldValue, newValue) -> newValue,
                LinkedHashMap::new
            )
        ));

List<对象>形式数据进行分组统计转变成Map<分组条件,数量统计>

mergeList.stream().collect(Collectors.groupingBy(
        WorkCalendarVO::getRemindDate,
        Collectors.counting()
));

将List对象中的某个List属性合并为一个新List

userList.stream()
        .flatMap(userVO -> userVO.getYhList().stream())
        .collect(Collectors.toList());