为了账号安全,请及时绑定邮箱和手机立即绑定

根据参数值和低于它的值获取hashmap键

根据参数值和低于它的值获取hashmap键

浮云间 2023-11-10 16:44:39
基本上我想用用户给定的值迭代哈希图,但也包括任何具有键的较小值“价格”。具有键的可能值为 1250、900、600、300。例如,如果用户输入 600,但有值 300 的键,则打印 600 和 300 值的所有键。这是我到目前为止所拥有的,但它只会打印给出的初始值的键,而不是较低的值。    private Map<Integer, Integer> prices = new HashMap<>();    private Map<Integer, Integer> bestPrices = new HashMap<>();    public void findBestPrice(LithiumPricing obj, Integer value)    {        //get hashmap prices from class LithiumPricing.        prices = obj.getPrices();        bestPrices.clear();        if (prices.containsValue(value)){            for (Map.Entry<Integer, Integer> entry : prices.entrySet()) {                if (entry.getValue() <= value){                     bestPrices.put(entry.getKey(), value);                }            }        }    } 
查看完整描述

1 回答

?
慕尼黑的夜晚无繁华

TA贡献1864条经验 获得超6个赞

您的代码的问题在于您将搜索值本身如下所示:

bestPrices.put(entry.getKey(), value);

相反,您必须按如下方式输入条目的值:

bestPrices.put(entry.getKey(), entry.getValue());


查看完整回答
反对 回复 2023-11-10
  • 1 回答
  • 0 关注
  • 63 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信