You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java中Map键转另一Map值时结果异常,所有键对应同一值的问题排查

Map键值转换后所有键对应同一值的问题

我尝试将一个Map的键转换为另一个Map的值,但最终生成的KeystoV Map中所有Integer类型的键都对应同一个Double类型的值(23.0),程序执行结果与预期不符。

代码实现

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class KeystoValues {

    public static void KtoV(Map<Double, Integer> Key) {
        

        Map<Double, Integer> List = new HashMap<Double, Integer>();
        Map<Integer, Double> KeystoV = new HashMap<Integer, Double>();
        
        System.out.println("List Map\n");
        
        List.putAll(Key);
        for(Map.Entry<Double, Integer> val : List.entrySet()) {
            System.out.println(val.getKey() + "," + val.getValue());
        }
            
        for(int h = 1; h<=List.size(); h++)
            for(Map.Entry<Double, Integer> convert : List.entrySet()) {
                Double j = convert.getKey();
                KeystoV.put(h, j);
            }   
        
        System.out.println("\nSet of keys in List Map now converted to set "
                + "of Values and stored to KeystoV Map\n\nKeystoV Map\n");
        
        for(Map.Entry<Integer, Double> converted : KeystoV.entrySet()) {
            System.out.println(converted.getKey() + "," + converted.getValue());
        }
        
        
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        
        ArrayList<Integer> Value = new ArrayList<Integer>();
        Map<Double, Integer> Key = new HashMap<Double, Integer>();
        
        Key.put(45.0,1);
        Key.put(40.0,2);
        Key.put(23.0,2);
        Key.put(25.0,3);
        Key.put(0.0,1);
        
        KtoV(Key);
        
    }

}

运行输出

List Map
        0.0,1
        25.0,3
        40.0,2
        45.0,1
        23.0,2

Set of keys in List Map now converted to set of Values and stored to KeystoV Map

KeystoV Map
        1,23.0
        2,23.0
        3,23.0
        4,23.0
        5,23.0

问题原因

核心错误在于嵌套循环的逻辑设计不当:

  • 外层循环遍历1到List.size()的整数h,作为KeystoV的键
  • 内层循环遍历List的所有entry,每次都用当前h作为键,把entry的key存入KeystoV
  • 由于内层循环会完整遍历所有entry,每个h对应的value会被多次覆盖,最终保留的是内层循环最后一次遍历到的entry的key(这里是23.0,HashMap的遍历顺序不固定,最后一个entry恰好是它)

修复方案

方案1:生成递增序号对应原Map键的新Map

如果你的需求是给原Map的每个entry分配一个递增的序号(1、2、3...)作为KeystoV的键,对应的值为原Map的key,只需要去掉外层循环,用一个自增计数器即可:

// 替换原嵌套循环的代码
int h = 1;
for(Map.Entry<Double, Integer> convert : List.entrySet()) {
    Double j = convert.getKey();
    KeystoV.put(h++, j);
}

修复后运行输出示例:

KeystoV Map
        1,0.0
        2,25.0
        3,40.0
        4,45.0
        5,23.0

方案2:用原Map的value作为新Map的键(处理重复键)

如果你的实际需求是将原Map的value作为KeystoV的键,原Map的key作为值,需要处理重复键的情况,可以改用Map<Integer, List<Double>>来存储:

// 修改KeystoV的类型
Map<Integer, List<Double>> KeystoV = new HashMap<>();

for(Map.Entry<Double, Integer> convert : List.entrySet()) {
    Integer key = convert.getValue();
    Double value = convert.getKey();
    // 若键不存在则创建新List,否则直接添加值
    KeystoV.computeIfAbsent(key, k -> new ArrayList<>()).add(value);
}

这种方式下,重复的value会被收集到同一个List中,输出示例:

KeystoV Map
        1,[0.0, 45.0]
        2,[40.0, 23.0]
        3,[25.0]

内容的提问来源于stack exchange,提问作者Najibu Tanimu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 09:20:40