【问题标题】:Java How to get the index of a Map of HashMap [duplicate]Java如何获取HashMap的Map的索引[重复]
【发布时间】:2016-04-22 18:42:09
【问题描述】:

如果它看起来很简单,但我无法轻易弄清楚,请原谅我。我考虑过使用循环,但想知道是否有人知道更简单的方法:我有:

Map<String, Integer> map = new HashMap<>();

        map.put("ClubB", 1);
        map.put("ClubA", 2);
        map.put("ClubC", 2);
        map.put("ClubD", 2);
        map.put("ClubE", 3);
        map.put("ClubF", 2);
        map.put("ClubG", 2);

我需要在我的测试中获取特定索引处的键或值。例如,我需要获取索引 3 处的键和值等。原因是我使用比较器并对 Map 进行排序,并希望显示特定索引处的值已更改。

感谢您的任何想法。

更新:

我用过:

HashMap leagueTable = new HashMap();
Map<String, Integer> map = sortByValues(leagueTable);

 public <K extends Comparable<K>, V extends Comparable<V>> Map<K, V> sortByValues(final Map<K, V> map) {
        Comparator<K> valueComparator = new Comparator<K>() {
            public int compare(K k1, K k2) {
                int compare = map.get(k2).compareTo(map.get(k1));
                if (compare == 0) {
                    return k1.compareTo(k2); // <- To sort alphabetically
                } else {
                    return compare;
                }
            }
        };
        Map<K, V> sortedByValues = new TreeMap<K, V>(valueComparator);
        sortedByValues.putAll(map);
        return sortedByValues;
    }

然后我使用 aloop 打印出值:

for (Map.Entry<String, Integer> entry : map.entrySet()) {

                    System.out.println( entry.getKey() + ", " + entry.getValue() );
} 

【问题讨论】:

  • 你说的是什么索引?处理地图时只有键和值。
  • 是的,我知道。我的意思是我想要例如键/值在第一个位置或第三个位置或第四个位置等的值,而不使用循环和计数器。
  • 地图未排序。也许LinkedHashMap我们你在找什么。
  • 我使用比较器订购了地图。只是想在特定索引处获取键或值,以显示在 JUnit 测试中排序后它已更改。
  • @AfshinGhazi a HashMap 没有订单。您不能“使用Comparator 订购Map”。请发布代码以显示您的具体操作。

标签: java dictionary indexing hashmap


【解决方案1】:

最终使用 for 循环并与我已经添加到地图中的内容进行比较:

HashMap<String, Integer> map = new HashMap<>();

        map.put("ClubD", 3);
        map.put("ClubB", 1);
        map.put("ClubA", 2);
        map.put("ClubC", 2);
        map.put("ClubE", 2);
        map.put("ClubF", 2);
        map.put("ClubG", 2);


        Map<String, Integer> mapResult = instance.sortByValues(map);
        String expectedResultKey = "ClubB";
        int expectedResultValue = 1;
        String resultKey = "";
        int resultValue = 0;
        for (Map.Entry<String, Integer> entry : map.entrySet()) {
        resultKey = entry.getKey();
        resultValue = entry.getValue();

        }

        assertSame(expectedResultKey, resultKey);

【讨论】:

    【解决方案2】:

    HashMap 的没有索引。他们只是将数据存储在键值对中。

    你为什么不使用二维数组而不是map 是一个地图? (并给它一个更合适的名字)

    String[][] array = new String[3][3];
    array[3] = new String[] { "ClubD" };
    array[1] = new String[] { "ClubB" };
    array[2] = new String[] { "ClubA", "ClubC", "ClubE", "ClubF", "ClubG" };
    
    System.out.println(array[3][0]);
    

    然后,如果您想循环该数组,您只需:

    for (int a = 0; a < array.length; a++)
        for (int b = 0; b < array[a].length; b++)
            if (array[a][b] != null)
                System.out.println("array["+a+"]["+b+"] is: "+array[a][b]);
    

    【讨论】:

      猜你喜欢
      • 2011-02-10
      • 2012-03-20
      • 2013-02-28
      • 1970-01-01
      • 1970-01-01
      • 2011-04-27
      • 2014-05-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多