简体   繁体   English

将一个哈希表的2个或更多键的整数值与另一个哈希表中的2个值进行比较

[英]Comparing integer values of 2 or more keys of one hashmap to 2 values in another hashmap

I have two hashmaps (resources and neededResources). 我有两个哈希图(资源和requiredResources)。
Goal is to reduce the amount of resources hashmap, but that can only happen if it has enough of both resource. 目标是减少哈希表的资源量,但这只有在两个资源都足够的情况下才能实现。

// String = name of resources available
// Integer = amount of resources available
Map<String, Integer> resources = new HashMap<>();
resources.put("gold", 10);
resources.put("silver", 10);
// String = name of resource needed
// Integer = amount of resource needed
Map<String, Integer> neededResources = new HashMap<>();
neededResources.put("gold", 2);
neededResources.put("silver", 3);

In this sample case, it would take resources 3 times since on 4th try there wouldn't be enough silver, gold value doesn't change as well. 在此示例中,由于第四次尝试将没有足够的白银​​,黄金价值也没有变化,因此将需要3倍的资源。

I'm a novice at Java. 我是Java的新手。 So far I've tried iterating through both of them but it becomes hard to read and my attempts have looked way too difficult for this task. 到目前为止,我已经尝试了遍历这两个方法,但是变得很难阅读,而且我的尝试看起来很难完成该任务。

Take the value from your resources map, substract the value from neededResources and check if it is >=0. 从资源映射中获取值,从requiredResources中减去该值,然后检查其是否> = 0。 This tells you that there were at least as many resources available as needed. 这告诉您至少有所需的可用资源。 If resources were available, update the value. 如果有资源,请更新值。 Otherwise dont. 否则不要。

Create a method wrapper in your resources class 在资源类中创建方法包装

public boolean hasResources(String... pResources){
  for(String resourceKey : pResources){
  int newValue = resources.get(pResourceKey) - neededResources.get(pResourceKey);
    if(newValue < 0){
       return false;
    }
  }
  return true;
}

public void takeResources(String... pResources){
  for(String resourceKey : pResources){
  int newValue = resources.get(pResourceKey) - neededResources.get(pResourceKey);
    resources.put(resourceKey, newValue);
  }
}

The key to this problem is that we don't want to modify the resources Map unless we are absolutely sure that it contains all of the resourcesNeeded . 这个问题的关键在于,除非我们完全确定它包含所有需要的resourcesNeeded ,否则我们不想修改resources Map Once you learn about Stream s in Java, this problem should simplify. 一旦您了解了Java中的Stream ,这个问题就会简化。

First, we want to check if the resources Map contains all of the keys in the resourcesNeeded Map , which may or may not already be given information which you haven't mentioned. 首先,我们要检查,如果resources Map包含了所有的按键resourcesNeeded Map ,这可能会或可能不会已经给一个你没有提到的信息。

resources.keySet().containsAll(neededResources.keySet());

If that condition is false , then we know that there are needed resources that are not available. 如果该条件为false ,则我们知道存在一些不可用的资源。 Otherwise, we can now check if every value in resources is greater than or equal to the value in resourcesNeeded for its respective key: 否则,我们现在可以检查resources每个值是否大于或等于resourcesNeeded各自相应键所需的值:

resources.entrySet()
         .stream()
         .allMatch(entry -> entry.getValue() >= resourcesNeeded.get(entry.getKey()));

If that condition is false , then more resources are needed then are currently available. 如果该条件为false ,则需要更多资源,然后当前可用。 Otherwise, we can now modify resources , essentially subtracting each respective value in resourcesNeeded : 否则,我们现在可以修改resources ,基本上每个减去相应值resourcesNeeded

resources.replaceAll((k, v) -> v - resourcesNeeded.getOrDefault(k, 0)));

The last two statements can be placed inside of a loop to ensure that the minimum amount of resources remain. 可以将最后两个语句放在循环内部,以确保剩余最少的资源。

I think this might be what you are after. 我认为这可能是您追求的目标。 The code can be inserted after your setup code. 该代码可以插入您的设置代码之后。

Find the number of possible expenditures: 查找可能的支出数量:

int n = resources.entrySet().stream().mapToInt(entry -> entry.getValue()/neededResources.getOrDefault(entry.getKey(), 0)).min().getAsInt();
System.out.println(n);

Prints 3 版画3

Spend it: 花费:

resources.replaceAll((k,v) -> v-n*neededResources.getOrDefault(k,0));
System.out.println(resources);

Prints {gold=4, silver=1} 打印{gold = 4,silver = 1}

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

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