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

问题描述

所以这是我的hashmap

so this is my hashmap

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

我想按值粗体排序。为了澄清,外部hashMaps关键代表一个女孩出生的一年,内部hashmap代表映射到该名称的人气排名的名称。

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.

所以让我们说,2015年,Abigail的名字是47373个,这是当年最受欢迎的名字,我想返回1号bc是第一个名字。有没有办法以这种方式对hashmap进行排序?

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?

如何将内部hashmaps值转换成我可以轻松排序的数组列表?任何帮助?

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;
    }

}

然后可以将地图定义为 Map< Integer,List< NameCount>> 。请注意,上面的类如何定义基于名称的等式和散列码,因此如果您想查看名称是否在列表中,您可以创建一个 NameCount 它使用包含 compareTo 执行订单从更高的数量到更低,所以当获得给定年份的列表< NameCount> 时,您然后可以使用 Collections.sort(list),并请求具有相同名称的 NameCount 的索引。

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;

}

使用 TreeSet 映射值以保证唯一的名称条目始终保持排序,但请注意,并且不会让你获得索引。

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.

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

10-20 09:25