Java如何获取HashMap映射的索引

Java如何获取HashMap映射的索引,java,dictionary,indexing,hashmap,Java,Dictionary,Indexing,Hashmap,如果这看起来很简单,请原谅我,但我不容易理解。我曾想过使用循环,但想知道是否有人知道一种更简单的方法:我有: 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

如果这看起来很简单,请原谅我,但我不容易理解。我曾想过使用循环,但想知道是否有人知道一种更简单的方法:我有:

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);
Map Map=newhashmap();
地图放置(“ClubB”,1);
地图放置(“ClubA”,2);
地图放置(“ClubC”,2);
地图放置(“ClubD”,2);
地图放置(“ClubE”,3);
地图放置(“ClubF”,2);
地图放置(“ClubG”,2);
我需要在测试中获取特定索引处的键或值。例如,我需要获取索引3处的键和值等。原因是我使用了一个比较器并对映射进行排序,希望显示特定索引处的值已更改

谢谢你的建议

更新:

我用过:

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;
    }
HashMap leagueTable=newhashmap();
Map Map=排序值(联盟表);
公共地图排序值(最终地图){
比较器值比较器=新比较器(){
公共整数比较(kk1,kk2){
int compare=map.get(k2).compareTo(map.get(k1));
如果(比较==0){

返回k1。compareTo(k2);//使用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);
HashMap map=newhashmap();
地图放置(“ClubD”,3);
地图放置(“ClubB”,1);
地图放置(“ClubA”,2);
地图放置(“ClubC”,2);
地图放置(“ClubE”,2);
地图放置(“ClubF”,2);
地图放置(“ClubG”,2);
Map mapResult=instance.SortByValue(Map);
字符串expectedResultKey=“ClubB”;
int expectedResultValue=1;
字符串resultKey=“”;
int resultValue=0;
对于(Map.Entry:Map.entrySet()){
resultKey=entry.getKey();
resultValue=entry.getValue();
}
资产相同(expectedResultKey、resultKey);
HashMap的没有索引。它们只将数据存储在键值对中

与其将
map
作为映射,不如使用2D数组?(并给它一个更合适的名称)

然后,如果您想在该数组中循环,只需执行以下操作:

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]);
for(int a=0;a
你说的是什么索引?在处理地图时,只有键和值。是的,我知道。我的意思是,我希望在不使用循环和计数器的情况下,在第一个位置或第三个位置或第四个位置等的键/值。地图没有排序。也许我们是你要找的。我给我们排序了地图正在对比较器进行排序。只想获取特定索引处的键或值,以显示它在JUnit测试中排序后发生了更改。@AfshinGhazi a
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]);