English 中文(简体)
B. 在 j积和修改收集时建立临时缓冲的优势
原标题:advantage of creating a temporary buffer when iterating over and modifying a collection in java

我正在讨论并修改地图(由一组现有物体制作):

public class Dispenser {
   private Map<Ingredient, Integer> availableIngredients = 
         new EnumMap<Ingredient, Integer>(Ingredient.class);
   public void orderSandwich(SandwichType sandwichType) {
      Map<Ingredient, Integer> buffer = 
            new EnumMap<Ingredient, Integer>(availableIngredients);
      for (Map.Entry<Ingredient, Integer> entry : 
            sandwichType.getIngredients().entrySet()) {
         Integer currentUnits = buffer.get(entry.getKey());
         buffer.put(entry.getKey(), currentUnits - entry.getValue());
      }     
      availableIngredients.clear();
      availableIngredients.putAll(buffer);
   }
}

我想问,在这种情况下,是否有必要收集临时、方法-当地的资料buffer。 我指的是,它做的是罚款,但并不肯定它的好处。 我必须明确我的原始收集,并以缓冲收集的内容取而代之。 缓冲收集基本上是在循环内修改的实际地图。

由于没有缓冲收集(仅使用我的原始收集资料),因此,我很想知道,是否建议对 o采取某种做法?

许多人感谢就这方面的最佳做法提出任何建议。

最佳回答

页: 1 您可在<代码>上操作,可检索到:Ingredients。 一切都将是罚款。 无无用间接费用。

问题回答

在此情况下,你不必使用缓冲器,因为你在收集<条码>和称呼器......getIngredients(上调。 在修改<条码>时,可供参考 这些是单独收集的。

如果使用缓冲手段是个好主意,则替换原体的工作可以比以下更容易:

  availableIngredients = buffer;

没有必要更新原来的收集资料,只需从现在起使用新版本即可。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签