从 HashMap 中的数组中检索值的单行代码
Single line of code to retrieve a value from an Array in a HashMap
所以我在 Java 工作,我有一个 HashMap,它存储一个带有整数数组的字符串键。这是声明的样子:
public HashMap<String, int[]> playerPoints = new HashMap<String, int[]>();
我的问题是是否可以通过简单的代码行检索某个键的数组的第一个整数。我明白我可以这样做:
tempArray = playerPoints.get(String);
int firstValue = tempArray[0];
但是有没有一种方法可以在一行代码中完成?
你可以写成一行,例如默认0
int firstValue = playerPoints.isEmpty() ? 0 : playerPoints.get("String").length == 0 ? 0 : playerPoints.get("String")[0];
Integer[] myArr = {2, 3, 5};
HashMap<String, Integer[]> hashMap = new HashMap<>();
hashMap.put("el", myArr);
System.out.println(hashMap.get("el")[0].toString());
输出将是2
所以我在 Java 工作,我有一个 HashMap,它存储一个带有整数数组的字符串键。这是声明的样子:
public HashMap<String, int[]> playerPoints = new HashMap<String, int[]>();
我的问题是是否可以通过简单的代码行检索某个键的数组的第一个整数。我明白我可以这样做:
tempArray = playerPoints.get(String);
int firstValue = tempArray[0];
但是有没有一种方法可以在一行代码中完成?
你可以写成一行,例如默认0
int firstValue = playerPoints.isEmpty() ? 0 : playerPoints.get("String").length == 0 ? 0 : playerPoints.get("String")[0];
Integer[] myArr = {2, 3, 5};
HashMap<String, Integer[]> hashMap = new HashMap<>();
hashMap.put("el", myArr);
System.out.println(hashMap.get("el")[0].toString());
输出将是2