我如何 return 一个作为值存储在 HashMap 中的数组
How do I return an array that's being stored as a value in a HashMap
我试图在散列图中存储一个数组,这样散列图也是另一个散列图的子元素。
形象地表达我的意思:
parentHashMap <"myParentKey":childHashMap>
---childHashMap <"properties":myArray[]>
------myArray = ["value 1", "value 2", "etc"]
我之所以对存储解决方案感到厌烦,是因为我希望我的 childHashMap 对 parentHashMap 中的每个键具有不同的“属性”值。
myArray 不一定是 all 我要存储的属性,而是 属性 可以保存多个值(即 < "genresOfMusic" : "摇滚、金属、爵士、乡村">)
最终,我如何 return myArray 才能显示其内容?此外,如果能提出更好地格式化我的存储解决方案的建议,而不是嵌套映射,我们将不胜感激。
你不能'parse'一个数组,你只能解析字符串。
假设您确定键 hello
将映射到一个 int 数组,那么:
int[] v = (int[]) properties.get("hello");
System.out.println("The second value is: " + v[1]);
如果你不知道,你可以随时做:
Object o = properties.get("hello");
if (o.getClass().isArray()) {
// we get here if o is an array.
// (and a NullPointerException if 'hello' is unmapped.
Object secondValue = java.lang.reflect.Array.get(o, 1);
}
但总的来说,您的设计很糟糕。映射中的异构存储听起来像是您非常非常糟糕地重新发明了类型化对象的概念。只需制作一个 class,每个 'property' 都有一个字段。如果这些数据来自其他一些系统,这些系统不是特别 statically/nominally 类型的(例如,通过网络 API 的一堆 JSON)然后使用擅长处理的工具用它。例如,对于 JSON,您可以使用 jackson 或 gson。
如果我理解你的问题就像你将 String[] 的实例放入映射中一样,那么:
您必须将其转换回这种类型。可以这样做:
HashMap<String, Object> properties = new HashMap<>();
Object array = properties.get("Key_To_Array");
if (array instanceof String[]) {
String[] arrayElementsAsString = (String[]) array;
// Do something with Strings in array.
}
public static void main(String... args) {
Map<String, Object> properties = new HashMap<>();
properties.put("one", new int[] { 1, 2, 3 });
properties.put("two", new String[] { "4", "5", "6" });
properties.forEach((key, val) -> {
if (val == null)
System.out.format("key: %s is null\n", key);
else if (val.getClass().isArray()) {
String[] arr = new String[Array.getLength(val)];
for (int i = 0; i < arr.length; i++)
arr[i] = String.valueOf(Array.get(val, i));
System.out.format("key: %s is an array: %s\n", key, Arrays.toString(arr));
} else
System.out.format("key: %s is not an array: %s\n", key, val);
});
}
我试图在散列图中存储一个数组,这样散列图也是另一个散列图的子元素。
形象地表达我的意思:
parentHashMap <"myParentKey":childHashMap>
---childHashMap <"properties":myArray[]>
------myArray = ["value 1", "value 2", "etc"]
我之所以对存储解决方案感到厌烦,是因为我希望我的 childHashMap 对 parentHashMap 中的每个键具有不同的“属性”值。
myArray 不一定是 all 我要存储的属性,而是 属性 可以保存多个值(即 < "genresOfMusic" : "摇滚、金属、爵士、乡村">)
最终,我如何 return myArray 才能显示其内容?此外,如果能提出更好地格式化我的存储解决方案的建议,而不是嵌套映射,我们将不胜感激。
你不能'parse'一个数组,你只能解析字符串。
假设您确定键 hello
将映射到一个 int 数组,那么:
int[] v = (int[]) properties.get("hello");
System.out.println("The second value is: " + v[1]);
如果你不知道,你可以随时做:
Object o = properties.get("hello");
if (o.getClass().isArray()) {
// we get here if o is an array.
// (and a NullPointerException if 'hello' is unmapped.
Object secondValue = java.lang.reflect.Array.get(o, 1);
}
但总的来说,您的设计很糟糕。映射中的异构存储听起来像是您非常非常糟糕地重新发明了类型化对象的概念。只需制作一个 class,每个 'property' 都有一个字段。如果这些数据来自其他一些系统,这些系统不是特别 statically/nominally 类型的(例如,通过网络 API 的一堆 JSON)然后使用擅长处理的工具用它。例如,对于 JSON,您可以使用 jackson 或 gson。
如果我理解你的问题就像你将 String[] 的实例放入映射中一样,那么:
您必须将其转换回这种类型。可以这样做:
HashMap<String, Object> properties = new HashMap<>();
Object array = properties.get("Key_To_Array");
if (array instanceof String[]) {
String[] arrayElementsAsString = (String[]) array;
// Do something with Strings in array.
}
public static void main(String... args) {
Map<String, Object> properties = new HashMap<>();
properties.put("one", new int[] { 1, 2, 3 });
properties.put("two", new String[] { "4", "5", "6" });
properties.forEach((key, val) -> {
if (val == null)
System.out.format("key: %s is null\n", key);
else if (val.getClass().isArray()) {
String[] arr = new String[Array.getLength(val)];
for (int i = 0; i < arr.length; i++)
arr[i] = String.valueOf(Array.get(val, i));
System.out.format("key: %s is an array: %s\n", key, Arrays.toString(arr));
} else
System.out.format("key: %s is not an array: %s\n", key, val);
});
}