后端|map stream流根据key排序

import com.google.common.collect.Maps; import java.util.Map; /** * @program: map排序工具类 * @description: * @author: danrenying * @create: 2020/7/13 **/ public class MapOrderByUtils {/** * @Description: 根据map的key排序 * @Author danrenying * @Param: [map, isDesc: true:降序,false:升序] * @Return: java.util.Map * @Date 2020/7/13 */ public static , V> Map orderByKey(Map map, boolean isDesc) { if (map == null || map.isEmpty()) { return null; } Map result = Maps.newLinkedHashMap(); if (isDesc) { map.entrySet().stream().sorted(Map.Entry.comparingByKey().reversed()) .forEachOrdered(e -> result.put(e.getKey(), e.getValue())); } else { map.entrySet().stream().sorted(Map.Entry.comparingByKey()) .forEachOrdered(e -> result.put(e.getKey(), e.getValue())); } return result; } }

【后端|map stream流根据key排序】

    推荐阅读