如何对HashMap按键值排序—DailyNews1

HashMap存储每对键和值作为一个Entry<K,V>对象,键的每次插入,都会有值对应到散列映射上,生成一个Entry <K,V>对象。通过使用这个Entry <K,V>对象,我们可以根据值来排序HashMap。

步骤如下:
1、从HashMap恢复Entry集合:

Set<Map.Entry<Character,Integer>> mapEntries = hashMap.entrySet();

2、用上述Entries创建ArrayList(或者LinkedList,通过排序链表处理,速度更快):

List<Map.Entry<Character,Integer>> list = new ArrayList<>(hashMap.entrySet());
List<Map.Entry<String, Integer>> list = new ArrayList<>(map.entrySet());

3、自定义比较器来使用Collections.sort()方法排序:

Collections.sort(list, new Comparator<Map.Entry<Character, Integer>>() {
            @Override
            public int compare(Map.Entry<Character, Integer> t1, Map.Entry<Character, Integer> t2) {
                return t2.getValue() - t1.getValue();
            }
        });

4、打印出来,或者存储到新的映射中:

for(Map.Entry s : list) {
            s.getValue();
        }

几点注意⚠️
Scanner的next()和nextLine()方法。两者一个是能读取空格一个是不能读取空格
若先 next()再nextLine()则 后面的会读前面未读的回车符,读出来为空,
nextInt()一样,不读换行符。

DailyNews:

有 n 个字符串,每个字符串都是由 A-J 的大写字符构成。现在你将每个字符映射为一个 0-9 的数字,不同字符映射为不同的数字。这样每个字符串就可以看做一个整数,唯一的要求是这些整数必须是正整数且它们的字符串不能有前导零。现在问你怎样映射字符才能使得这些字符串表示的整数之和最大?

Answer:

利用权重来计算(若size=10,将权重最小的且不是头元素的第一个字符放置在映射值为0的位置)

输入例子:
ABC
BCA

设 table = A B C D E F G H I J
个位权值 为 1
十位权值 为 10
百位权值 为 100
依次类推, 没有出现的字母权值为 0

则 A 的权重为 100 + 1 = 101
B 的权重为 10 + 100 = 110
C 的权重为 1 + 10 = 11
D - J 的权重为 0

即 依照 table 的字母排列顺序,权重表为
weight = 101 110 11 0 0 0 0 0 0

进而得到
映射值表
ret = 8 9 7 0 0 0 0 0 0

将 (权重表 x 映射表),然后再累加起来就是题目要的答案

则这些数的和为 (101 * 8) + (110 * 9) + (11 * 7) + 0 + … + 0 = 1875

package LeetCode;

import java.util.*;

public class DailyNews1 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        ArrayList headlist = new ArrayList<Character>();
        HashMap<Character,Integer> hm = new HashMap<>();

        long sum=0;
        while (scanner.hasNext()) {
            int n = scanner.nextInt();
            String[] strs = new String[n];
            for(int i=0; i<n; i++) {
               // strs[i] = scanner.nextLine();
                strs[i] = scanner.next();
            }
            System.out.println(Findsum(n,strs));
        }



    }

    public static long Findsum(int n, String[] strings) {
        ArrayList headlist = new ArrayList<Character>();
        HashMap<Character,Integer> hashMap = new HashMap<>();
        Set<Map.Entry<Character,Integer>> mapEntries = hashMap.entrySet();

        for(int i=0; i<n; i++) {
            setWeights(strings[i],headlist,hashMap);
        }

        List<Map.Entry<Character,Integer>> list = new ArrayList<>(hashMap.entrySet());
        Collections.sort(list, new Comparator<Map.Entry<Character, Integer>>() {
            @Override
            public int compare(Map.Entry<Character, Integer> t1, Map.Entry<Character, Integer> t2) {
                return t2.getValue() - t1.getValue();
            }
        });

        if(list.size()==10) {
            for(int i=9; i>=0; i--) {
                if(!headlist.contains(list.get(i).getKey())) {
                    list.remove(i);
                    break;
                }
            }
        }

        int sum = 0;
        int w = 9;
        for(Map.Entry s : list) {
            sum += w * (int)s.getValue();
            w--;
        }

        return sum;
    }

    public static void setWeights(String string, ArrayList<Character> headlist, HashMap<Character,Integer> map) {
        char[] chars = string.toCharArray();
        int weight = 1;
        for(int i=chars.length-1; i>=0; i--) {
            if(map.containsKey(chars[i])) {
                map.put(chars[i],map.get(chars[i]) + weight);
            } else {
                map.put(chars[i], weight);
            }
            weight *= 10;
        }
        System.out.println(chars);
        //headlist.add(chars[0]);
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值