载入天数...载入时分秒...

常用工具类

1 List<Map<K,V>>转化为 Map<K,List<V>>

static <K,V> Map<K,List<V>> getMapFromTheList(List<Map<K,V>> list){
    return list.stream()
            .flatMap(map -> map.entrySet().stream())
            .collect(Collectors.groupingBy(
                    Map.Entry::getKey, Collectors.mapping(Map.Entry::getValue, Collectors.toList())));
}

e.g. List<Map<String,Object>> -> Map<String,List<Object>>

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class Main {
    static <K,V> Map<K,List<V>> getMapFromTheList(List<Map<K,V>> list){
        return list.stream()
                .flatMap(map -> map.entrySet().stream())
                .collect(Collectors.groupingBy(
                        Map.Entry::getKey, Collectors.mapping(Map.Entry::getValue, Collectors.toList())));
    }
    public static void main(String[] args) {
        List<Map<String,Object>> list = new ArrayList<>();
        Map<String,Object> map = new HashMap<>();
        map.put("date","2021-06-03");
        map.put("weather","晴");
        map.put("wind","西北风3-4级");
        map.put("temp","17-29°C");
        list.add(map);

        map = new HashMap<>();
        map.put("date","2021-06-04");
        map.put("weather","晴");
        map.put("wind","北风3-4级");
        map.put("temp","17-30°C");
        list.add(map);
        map = new HashMap<>();
        map.put("date","2021-06-05");
        map.put("weather","多云");
        map.put("wind","西北风3-4级");
        map.put("temp","18-32°C");
        list.add(map);

        map = new HashMap<>();
        map.put("date","2021-06-06");
        map.put("weather","多云");
        map.put("wind","东北风3级");
        map.put("temp","17-29°C");
        list.add(map);

        System.out.println(getMapFromTheList(list));


    }
}

{date=[2021-06-03, 2021-06-04, 2021-06-05, 2021-06-06], temp=[17-29°C, 17-30°C, 18-32°C, 17-29°C], weather=[晴, 晴, 多云, 多云], wind=[西北风3-4级, 北风3-4级, 西北风3-4级, 东北风3级]}

2 以相同K 合并两个List<Map<K,V>>为一个List<Map<K,V>>,并根据KV排序

 static List<Map<String, Object>> merge(List<Map<String, Object>> m1, List<Map<String, Object>> m2, String key) {
        m1.addAll(m2);
        Set<String> set = new HashSet<>();
        List<Map<String, Object>> list = m1.stream().collect(Collectors.groupingBy(o -> {
            set.addAll(o.keySet());
            return o.get(key);
        })).values().stream().map(maps -> {
            Map<String, Object> map = maps.stream().flatMap(m -> {
                return m.entrySet().stream();
            }).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (a, b) -> b));
            set.forEach(k -> {
                if (!map.containsKey(k)) {
                    map.put(k, 0);
                }
            });
            return map;
        }).sorted(Comparator.comparing(map -> map.get(key).toString())).collect(Collectors.toList());

        return list;

    }

e.g. 两个List<Map<String,Object>> 根据date合并并排序 -> List<Map<String,Object>>

注意日期应该是date等格式,我例子是String

import java.util.*;
import java.util.stream.Collectors;

public class Main {
 static List<Map<String, Object>> merge(List<Map<String, Object>> m1, List<Map<String, Object>> m2, String key) {
        m1.addAll(m2);
        Set<String> set = new HashSet<>();
        List<Map<String, Object>> list = m1.stream().collect(Collectors.groupingBy(o -> {
            set.addAll(o.keySet());
            return o.get(key);
        })).values().stream().map(maps -> {
            Map<String, Object> map = maps.stream().flatMap(m -> {
                return m.entrySet().stream();
            }).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (a, b) -> b));
            set.forEach(k -> {
                if (!map.containsKey(k)) {
                    map.put(k, 0);
                }
            });
            return map;
        }).sorted(Comparator.comparing(map -> map.get(key).toString())).collect(Collectors.toList());

        return list;

    }

    public static void main(String[] args) {
        List<Map<String,Object>> list1 = new ArrayList<>();
        List<Map<String,Object>> list2 = new ArrayList<>();

        Map<String,Object> map1 = new HashMap<>();
        Map<String,Object> map2 = new HashMap<>();
        Map<String,Object> map3 = new HashMap<>();
        Map<String,Object> map4 = new HashMap<>();

        map1.put("date","2021-06-03");
        map1.put("weather","晴");
        map1.put("wind","西北风3-4级");
        map1.put("temp","17-29°C");
        list1.add(map1);

        map2.put("date","2021-06-04");
        map2.put("weather","晴");
        map2.put("wind","北风3-4级");
        map2.put("temp","17-30°C");
        list1.add(map2);

        map3.put("date","2021-06-03");
        map3.put("city","长安");
        list2.add(map3);

        map4.put("date","2021-06-04");
        map4.put("city","幽州");
        list2.add(map4);

        List<Map<String, Object>> list = merge(list1,list2,"date");
        System.out.println(list);
    }
}
[{date=2021-06-03, temp=17-29°C, city=长安, weather=晴, wind=西北风3-4级}, {date=2021-06-04, temp=17-30°C, city=幽州, weather=晴, wind=北风3-4级}]

3 将List<Map<K,V>>K 全部转化为大写或者小写(不传参数或者1默认大写,其他小写)

    static List<Map<String,Object>>  convertKeyCase (List<Map<String,Object>> list,int...s) {

        return list.stream()
                .map(m -> m.entrySet().stream()
                .sorted(Map.Entry.comparingByKey())
                .collect(Collectors.toMap(p -> s.length == 0 || s[0]==1 ? p.getKey().toUpperCase():p.getKey().toLowerCase(), 
                        Map.Entry::getValue,
                        (v1,v2)->v1,
                        LinkedHashMap::new)))
                .collect(Collectors.toList());

    }

e.g. 根据K排序并使用LinkedHashMap 保证插入顺序,可替换为TreeMap

import java.util.*;
import java.util.stream.Collectors;

public class Test {

    static List<Map<String,Object>>  convertKeyCase (List<Map<String,Object>> list,int...s) {

        return list.stream()
                .map(m -> m.entrySet().stream()
                .sorted(Map.Entry.comparingByKey())
                .collect(Collectors.toMap(p -> s.length == 0 || s[0]==1 ? p.getKey().toUpperCase():p.getKey().toLowerCase(),
                        Map.Entry::getValue,
                        (v1,v2)->v1,
                        LinkedHashMap::new)))
                .collect(Collectors.toList());

    }
    public static void main(String[] args) {
        List<Map<String,Object>> list1 = new ArrayList<>();

        Map<String,Object> map1 = new HashMap<>();
        Map<String,Object> map2 = new HashMap<>();

        map1.put("weather","1");
        map1.put("date","2021-06-03");
        map1.put("wind","2-3");
        map1.put("temp","17-29°C");
        map1.put("a","18");
        list1.add(map1);

        map2.put("weather","1");
        map2.put("date","2021-06-04");
        map2.put("wind","3-4");
        map2.put("temp","17-30°C");
        map2.put("a","20");
        list1.add(map2);

        List<Map<String,Object>> list = convertKeyCase(list1);
        List<Map<String,Object>> list2 = convertKeyCase(list1,0);
        System.out.println(list);
        System.out.println(list2);
    }
}
[{A=18, DATE=2021-06-03, TEMP=17-29°C, WEATHER=1, WIND=2-3}, {A=20, DATE=2021-06-04, TEMP=17-30°C, WEATHER=1, WIND=3-4}]
[{a=18, date=2021-06-03, temp=17-29°C, weather=1, wind=2-3}, {a=20, date=2021-06-04, temp=17-30°C, weather=1, wind=3-4}]
posted @ 2021-06-03 17:29  旧信  阅读(102)  评论(0编辑  收藏  举报