如何避免在使用 Maps 时重复使用 put() 方法?

所以基本上我有这个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


UYOU
浏览 229回答 3
3回答

墨色风雨

在任何 Java 版本中,您都可以这样做:Double[] orange = {11.7, 0.9, 0.1,&nbsp; 4.0, 89.0,&nbsp; &nbsp;1.0, 0.0,&nbsp; 1.0, 2.0,&nbsp; &nbsp;0.1, 4.0, 5.0,&nbsp; &nbsp; 47.0};Double[] broccoli = {7.2, 2.4, 0.4,&nbsp; 31.0,108.0, 7.0,176.0,30.0, 45.0, 23.0, 4.0, 3.0,&nbsp; &nbsp; 11.0};String[]&nbsp; &nbsp;keys&nbsp; &nbsp;= {"orange", "broccoli"};Double[][] values = {orange&nbsp; , broccoli&nbsp; };Map<String, Double[]> map = new HashMap<>();for (int i = 0; i < keys.length; i++)&nbsp; &nbsp; map.put(keys[i], values[i]);在 Java 9+ 中,如果你有 10 个或更少的映射条目,你可以像这样简化它:Double[] orange = {11.7, 0.9, 0.1,&nbsp; 4.0, 89.0,&nbsp; &nbsp;1.0, 0.0,&nbsp; 1.0, 2.0,&nbsp; &nbsp;0.1, 4.0, 5.0,&nbsp; &nbsp; 47.0};Double[] broccoli = {7.2, 2.4, 0.4,&nbsp; 31.0,108.0, 7.0,176.0,30.0, 45.0, 23.0, 4.0, 3.0,&nbsp; &nbsp; 11.0};Map<String, Double[]> map = Map.of(&nbsp; &nbsp; &nbsp; &nbsp; "orange"&nbsp; , orange,&nbsp; &nbsp; &nbsp; &nbsp; "broccoli", broccoli );如果你不需要Double[]被命名,你可以内联它们:Map<String, Double[]> map = Map.of(&nbsp; &nbsp; &nbsp; &nbsp; "orange", new Double[] {11.7, 0.9, 0.1,&nbsp; 4.0, 89.0,&nbsp; &nbsp;1.0, 0.0,&nbsp; 1.0, 2.0,&nbsp; &nbsp;0.1, 4.0, 5.0,&nbsp; &nbsp; 47.0},&nbsp; &nbsp; &nbsp; &nbsp; "broccoli", new Double[] {7.2, 2.4, 0.4,&nbsp; 31.0,108.0, 7.0,176.0,30.0, 45.0, 23.0, 4.0, 3.0,&nbsp; &nbsp; 11.0} );

侃侃尔雅

您可以创建包含name和(列表)的类nutrients:import java.util.*;public class Main {&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; Fruit orange = new Fruit(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "orange",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 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}&nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; &nbsp; &nbsp; Fruit broccoli = new Fruit(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "broccoli",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 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}&nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; &nbsp; &nbsp; List<Fruit> fruitList = new ArrayList<>(Arrays.asList(orange, broccoli));&nbsp; &nbsp; &nbsp; &nbsp; Map<String, Double[]> map = new HashMap<>();&nbsp; &nbsp; &nbsp; &nbsp; for (Fruit fruit : fruitList) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; map.put(fruit.getName(), fruit.getNutrients());&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}class Fruit {&nbsp; &nbsp; private String name;&nbsp; &nbsp; private Double[] nutrients;&nbsp; &nbsp; Fruit(String name, Double[] nutrients) {&nbsp; &nbsp; &nbsp; &nbsp; this.name = name;&nbsp; &nbsp; &nbsp; &nbsp; this.nutrients = nutrients;&nbsp; &nbsp; }&nbsp; &nbsp; public String getName() {&nbsp; &nbsp; &nbsp; &nbsp; return name;&nbsp; &nbsp; }&nbsp; &nbsp; public Double[] getNutrients() {&nbsp; &nbsp; &nbsp; &nbsp; return nutrients;&nbsp; &nbsp; }}

宝慕林4294392

直到 Java-8,Java API 还没有提供标准方法来放置多个键值而不需要put多次使用方法。但是 Java-9 API 提供了一个工厂方法Map#of,您可以使用它通过传递键值来构建您的地图。Map.of("<Key1>",&nbsp;"<Value1>",&nbsp;"<Key2>",&nbsp;"<Value2>");注意:Map#of返回一个不可变的映射。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java