【问题标题】:How do I avoid repeating the use of the put() method while using Maps?如何避免在使用 Maps 时重复使用 put() 方法?
【发布时间】:2019-03-09 19:37:45
【问题描述】:

所以基本上我有这个 HashMap:

HashMap<String, Double[]> list = new HashMap<>();

和值(我想保存的食物的宏观和微量营养素):

Double[] orange = {11.7, 0.9, 0.1, 4.0, 89.0, 1.0, 0.0, 1.0, 2.0, 0.1, 4.0, 5.0, 47.0};

Double[] broccoli = {7.2, 2.4, 0.4, 31.0, 108.0, 7.0, 176.0, 30.0, 45.0, 23.0, 4.0, 3.0, 11.0};

list.put("orange", orange);

list.put("broccoli", broccoli);

现在我有比这些更多的食物,所以我想将所有“list.put()”缩小为一个。也许使用 for 循环。并使用另一个带有食物名称的字符串数组在 for 上迭代每个。关于如何组织我想在这里做的任何提示?谢谢。

如果需要,我的其余代码在这里:https://pastebin.com/Vw2UerDG

【问题讨论】:

  • 为什么你的地图命名为list
  • 将所有值(及其名称)存储在一个文件中。解析文件以创建您的Map。避免硬编码幻数。
  • 我把它命名为列表是我的错误。

标签: java list hashmap put repeat


【解决方案1】:

在任何 Java 版本中,您都可以这样做:

Double[] orange = {11.7, 0.9, 0.1,  4.0, 89.0,   1.0, 0.0,  1.0, 2.0,   0.1, 4.0, 5.0,    47.0};
Double[] broccoli = {7.2, 2.4, 0.4,  31.0,108.0, 7.0,176.0,30.0, 45.0, 23.0, 4.0, 3.0,    11.0};

String[]   keys   = {"orange", "broccoli"};
Double[][] values = {orange  , broccoli  };

Map<String, Double[]> map = new HashMap<>();
for (int i = 0; i < keys.length; i++)
    map.put(keys[i], values[i]);

在 Java 9+ 中,如果您有 10 个或更少的映射条目,您可以像这样简化它:

Double[] orange = {11.7, 0.9, 0.1,  4.0, 89.0,   1.0, 0.0,  1.0, 2.0,   0.1, 4.0, 5.0,    47.0};
Double[] broccoli = {7.2, 2.4, 0.4,  31.0,108.0, 7.0,176.0,30.0, 45.0, 23.0, 4.0, 3.0,    11.0};

Map<String, Double[]> map = Map.of(
        "orange"  , orange,
        "broccoli", broccoli );

如果您不需要命名 Double[],您可以内联它们:

Map<String, Double[]> map = Map.of(
        "orange", new Double[] {11.7, 0.9, 0.1,  4.0, 89.0,   1.0, 0.0,  1.0, 2.0,   0.1, 4.0, 5.0,    47.0},
        "broccoli", new Double[] {7.2, 2.4, 0.4,  31.0,108.0, 7.0,176.0,30.0, 45.0, 23.0, 4.0, 3.0,    11.0} );

【讨论】:

    【解决方案2】:

    在 Java-8 之前,Java API 还没有提供标准方法来放置多个键值,而无需多次使用 put 方法。但是 Java-9 API 提供了一个工厂方法 Map#of ,您可以使用它通过传递键值来构建您的地图。

    Map.of("<Key1>", "<Value1>", "<Key2>", "<Value2>");
    

    注意:Map#of 返回一个不可变的映射。

    【讨论】:

      【解决方案3】:

      您可以创建包含name 和(列表)nutrients 的类:

      import java.util.*;
      
      public class Main {
      
          public static void main(String[] args) {
              Fruit orange = new Fruit(
                      "orange",
                      new Double[]{0.9, 0.1, 4.0, 89.0, 1.0, 0.0, 1.0, 2.0, 0.1, 4.0, 5.0, 47.0}
              );
              Fruit broccoli = new Fruit(
                      "broccoli",
                      new Double[]{7.2, 2.4, 0.4, 31.0, 108.0, 7.0, 176.0, 30.0, 45.0, 23.0, 4.0, 3.0, 11.0}
              );
      
              List<Fruit> fruitList = new ArrayList<>(Arrays.asList(orange, broccoli));
      
              Map<String, Double[]> map = new HashMap<>();
      
              for (Fruit fruit : fruitList) {
                  map.put(fruit.getName(), fruit.getNutrients());
              }
      
          }
      }
      
      class Fruit {
      
          private String name;
          private Double[] nutrients;
      
          Fruit(String name, Double[] nutrients) {
              this.name = name;
              this.nutrients = nutrients;
          }
      
          public String getName() {
              return name;
          }
      
          public Double[] getNutrients() {
              return nutrients;
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2021-11-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-01-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多