简体   繁体   English

JAVA - HashMap 检查特定键值(如果更大)

[英]JAVA - HashMap check specific key value (if greater)

Map<Integer, Integer> listInfo = new HashMap<Integer, Integer>();

I have been trying to search from Google, but I don't really know why I cannot find a correct solution.我一直在尝试从 Google 搜索,但我真的不知道为什么找不到正确的解决方案。 Anyways my HashMap will store two Integers.无论如何,我的 HashMap 将存储两个整数。 I would use normal "int[]" string-list or whatever it is called for, but it will return me exception something about "array outofbounds" or something related to that, because it's index will be greater than int.我会使用普通的“int[]”字符串列表或它所要求的任何内容,但它会返回有关“数组越界”或与之相关的异常信息,因为它的索引将大于 int。 Not sure, but anyways let's stay in the topic.不确定,但无论如何让我们继续讨论这个话题。

My english is pretty bad, but if you don't understand what I'm talking about hope this helps.我的英语很差,但如果你不明白我在说什么希望这会有所帮助。 (Just the idea, not based in real code) (只是想法,不是基于实际代码)

if(listInfo.containsKey(key)) {
    if(listInfo.getKeyValue(key).valueOfKey() > valueToBeAdded) {
        listInfo.put(key, valueToBeAdded);
    }
} else {
    listInfo.put(key, valueToBeAdded);
}

I have tried similar way than above, but with pretty much correct functions, but it will conflict, because it says that it cannot compare key value with int, because key value is object?我尝试过与上面类似的方法,但功能几乎正确,但它会发生冲突,因为它说它无法将键值与 int 进行比较,因为键值是对象? Why it's object, because I've defined that it should be Integer?为什么它是对象,因为我已经定义它应该是整数? I've also tried for(Entry entry : ......) loop, but I have no clue how I can get specific key's value (I am not talking about key value, I'm talking about value that the specific key holds)我也试过 for(Entry entry : ......) 循环,但我不知道如何获得特定键的值(我不是在谈论键值,我在谈论特定键的值持有)

I want only update the value that the specific (existing) key holds if the value that key holds is greater and the value that will be added is less than the current one.我只想更新特定(现有)键持有的值,如果该键持有的值更大并且将添加的值小于当前值。

Find below a snippet which does what you are looking for (assuming that I right understood your intention).在下面找到一个代码片段,它可以满足您的需求(假设我正确理解了您的意图)。

Map<Integer, Integer> listInfo = new HashMap<>();
listInfo.put(1, 23);
listInfo.put(2, 45);
Integer valueToBeAdded = 42;
System.out.println("listInfo = " + listInfo);
if (listInfo.containsKey(1)) {
    if (listInfo.get(1) < valueToBeAdded) {
        listInfo.put(1, valueToBeAdded);
    }
} else {
    listInfo.put(1, valueToBeAdded);
}
System.out.println("listInfo = " + listInfo);

output输出

listInfo = {1=23, 2=45} // the initial listInfo entries (key, value)
listInfo = {1=42, 2=45} // after the value for key `1` has been updated

Looks like you want to only put values that are smaller.看起来您只想放置较小的值。
For this you can use Map.compute :为此,您可以使用Map.compute

final HashMap<Integer, Integer> map = new HashMap<Integer,Integer>();
map.put(123, 456);
System.out.println(map);
final int x = 234;
final BiFunction<? super Integer, ? super Integer, ? extends Integer> f =
    (k, v) -> v == null ? x : Math.min(v, x);
map.compute(123, f);
map.compute(999, f);
System.out.println(map);

Sadly, Java doesn't really support functional programming.遗憾的是,Java 并不真正支持函数式编程。 It would be nice to have an easy way for partial application.有一个简单的方法来部分应用会很好。 Here's a version with a static method that partially applies some value and returns a BiFunction ( f is a higher-order function).这是一个带有静态方法的版本,它部分地应用了一些值并返回一个BiFunctionf是一个高阶函数)。

  public static void main(final String[] arrg) {
    final HashMap<Integer, Integer> map = new HashMap<Integer,Integer>();
    map.put(123, 456);
    System.out.println(map);
    map.compute(123, f(345));
    map.compute(123, f(99999));
    map.compute(999, f(888));
    System.out.println(map);
  }

  static BiFunction<? super Integer, ? super Integer, ? extends Integer> f(final int x) {
    return (k, v) -> v == null ? x : Math.min(v, x);
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM