排序哈希图谁的键是另一个哈希图 [英] sorting a hashmap who's key is another hashmap

查看:38
本文介绍了排序哈希图谁的键是另一个哈希图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以这是我的哈希图

 public HashMap>女孩 =new HashMap>();;

我想按值对粗体进行排序.为了澄清起见,外部 hashMaps 键代表女孩出生的年份,内部 hashmap 代表映射到该名称流行度排名的名称.

假设在 2015 年,Abigail 的名字被授予了 47373 个婴儿,它是那一年最受欢迎的名字,我想返回数字 1,因为它是排名第一的名字.有没有办法以这种方式对哈希图进行排序?

我如何将内部哈希映射值转换为我可以轻松排序的数组列表?有什么帮助吗?

解决方案

您最好只为名称和出现次数创建一个类.

import java.util.Objects;公共类 NameCount 实现了 Comparable{私有最终字符串名称;私有整数计数;公共名称计数(字符串名称){this.name = 名称;计数 = 0;}公共名称计数(字符串名称,整数计数){this.name = 名称;this.count = 计数;}公共 int getCount() {返回计数;}public void setCount(int count) {this.count = 计数;}公共无效增量计数(){计数++;}@覆盖公共 int hashCode() {返回 Objects.hashCode(this.name);}@覆盖公共布尔等于(对象 obj){if(obj == null) 返回假;if(getClass() != obj.getClass()) 返回假;final NameCount other = (NameCount)obj;if(!Objects.equals(this.name, other.name)) 返回假;返回真;}@覆盖公共 int compareTo(NameCount o) {返回 o.count - 计数;}}

然后您可以将您的地图定义为 Map>.请注意上面的类如何仅根据名称定义相等性和哈希码,因此如果您想查看名称是否在列表中,您只需为其创建一个 NameCount 并使用 包含.compareTo 实现顺序从高到低,所以当获取给定年份的 List 时,你可以使用 Collections.sort(list) 并询问具有相同名称的 NameCount 的索引.

public void test(Map> map) {整数年 = 2017 年;列表list = map.get(year);//使用时首先对列表进行空检查...Collections.sort(list);NameCount check = new NameCount("Abigail");int rank = list.indexOf(check) + 1;}

使用 TreeSet 映射值来保证名称条目的唯一性始终保持排序似乎更有意义,但请注意 TreeSet 定义了基于比较的相等性,而不是相等性,它不会让您获得索引.>

so this is my hashmap

   public HashMap<Integer, HashMap<String, Integer>> girls =  
          new HashMap<Integer, HashMap<String, **Integer**>>();;

I want to sort the bolded by value. For clarification the outer hashMaps key stands for a year a girl child was born and the inner hashmap stands for a name mapped to the popularity ranking of the name.

So let's say that in 2015, the name Abigail was given to 47373 babies and it was the most popular name in that year, I'd want to return the number 1 bc it's the number one name. Is there any way to sort a hashmap in this way?

how would I turn the inner hashmaps values into an arraylist that I could then easily sort? Any help?

解决方案

You're better off just creating a class for a name and number of occurrences.

import java.util.Objects;

public class NameCount implements Comparable<NameCount> {

    private final String name;
    private int count;

    public NameCount(String name) {
        this.name = name;
        count = 0;
    }

    public NameCount(String name, int count) {
        this.name = name;
        this.count = count;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public void incrementCount() {
        count++;
    }

    @Override
    public int hashCode() {
        return Objects.hashCode(this.name);
    }

    @Override
    public boolean equals(Object obj) {
        if(obj == null) return false;
        if(getClass() != obj.getClass()) return false;
        final NameCount other = (NameCount)obj;
        if(!Objects.equals(this.name, other.name)) return false;
        return true;
    }

    @Override
    public int compareTo(NameCount o) {
        return o.count - count;
    }

}

You can then define your map as Map<Integer, List<NameCount>>. Note how the above class defines equality and hash code based only on the name, so if you want to see if a name is in a list, you can just create a NameCount for it and use contains. The compareTo implementation orders from higher count to lower, so when getting the List<NameCount> for a given year, you can then use Collections.sort(list) on it and ask for the index for a NameCount with the same name.

public void test(Map<Integer, List<NameCount>> map) {

    int year = 2017;
    List<NameCount> list = map.get(year);
    // Do null-check on list first when using this...
    Collections.sort(list);
    NameCount check = new NameCount("Abigail");
    int rank = list.indexOf(check) + 1;

}

It might seem to make more sense to use TreeSet map values to guarantee unique name entries and keep them sorted all the time, but note that TreeSet defines equality based on comparison, not equals, and it wouldn't let you get the index.

这篇关于排序哈希图谁的键是另一个哈希图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆