zoukankan      html  css  js  c++  java
  • 通过反射,获取linkedHashMap的最后一个键值对。对map按照值进行排序。

    1:通过反射,获取linkedHashMap的最后一个键值对。

    Map<Integer, Integer> map = new LinkedHashMap<>();            
    Field tail = map.getClass().getDeclaredField("tail");
                tail.setAccessible(true);
                Map.Entry<Integer,Integer> entry=(Map.Entry<Integer, Integer>) tail.get(map);
                Integer key = entry.getKey();
                Integer value = entry.getValue();

     2: 对Map按照值进行进行排序

    public static <K, V extends Comparable<? super V>> Map<K, V> sortByValue(Map<K, V> map) {
            Map<K, V> result = new LinkedHashMap<>();
            Stream<Map.Entry<K, V>> st = map.entrySet().stream();
    
            st.sorted(Comparator.comparing(e -> e.getValue())).forEach(e -> result.put(e.getKey(), e.getValue()));
    
            return result;
        }

     3:根据键对map进行排序

    package com.li.jingdong;
    
    import java.util.Comparator;
    import java.util.LinkedHashMap;
    import java.util.Map;
    import java.util.stream.Stream;
    
    /**
     * @program: GradleTestUseSubModule
     * @author: Yafei Li
     * @create: 2018-09-10 10:37
     **/
    public class Main5 {
    
        public static void salaryfrequeny(int num,int[] salaries) throws NoSuchFieldException, IllegalAccessException {
            Map<Integer, Integer> map = new LinkedHashMap<>();
            for (int i = 0; i < num; i++) {
                map.computeIfPresent(salaries[i],(k,v)->{
                    return v+1;
                });
                map.putIfAbsent(salaries[i], 1);
            }
            Map<Integer, Integer> sortMap = sortByKey(map);
            System.out.println(sortMap);
        }
    
        //根据值对map进行排序
        public static <K, V extends Comparable<? super V>> Map<K, V> sortByValue(Map<K, V> map) {
            Map<K, V> result = new LinkedHashMap<>();
            Stream<Map.Entry<K, V>> st = map.entrySet().stream();
            st.sorted(Comparator.comparing(e -> e.getValue())).forEach(e -> result.put(e.getKey(), e.getValue()));
            return result;
        }
        //根据键对map排序
        public static <K extends Comparable,V> Map<K,V> sortByKey(Map<K,V> map) {
            Map<K, V> resultMap = new LinkedHashMap<>();
            Stream<Map.Entry<K, V>> stream = map.entrySet().stream();
            //(a,b)->b.getKey().compareTo(a.getKey())  是一个比较器
            stream.sorted((a,b)->b.getKey().compareTo(a.getKey())).forEach(e->{   //e就是挨个取出map中的Entry,
                System.out.println("key:"+e.getKey()+"  value:"+e.getValue());
                resultMap.put(e.getKey(), e.getValue());
            });
            return resultMap;
        }
    
    
        public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {
    
            int n=5;
            int[] salary = {1000, 2000, 1000, 3000, 2000};
            salaryfrequeny(n,salary);
        }
    }

    结果:

    key:3000  value:1
    key:2000  value:2
    key:1000  value:2
    {3000=1, 2000=2, 1000=2}
  • 相关阅读:
    STL: merge
    STL: rotate
    javascript的prototype继承问题
    日期正则表达式
    有关linq的一系列学习的文章,值得收藏
    EF读取关联数据
    jQuery UI中的日期选择插件Datepicker
    LINQ的基本语法中八个关键字用法说明
    Shell变量内容的删除、替代与替换
    Shell命令别名与历史命令
  • 原文地址:https://www.cnblogs.com/liyafei/p/9620894.html
Copyright © 2011-2022 走看看