[英]How to retrieve value from Map<ArrayList<String>, Object>
I want to know how can I retrieve values from Map<ArrayList<String>, Object>
Where the String and object are stored as Array [] for user define length.我想知道如何从
Map<ArrayList<String>, Object>
中检索值,其中 String 和 object 存储为 Array [] 以供用户定义长度。
Here is an Example:这是一个例子:
int counter=0, n=0;
if (dataSnapshot.exists()){
for (DataSnapshot ds: dataSnapshot.getChildren()){
loca[counter]= new ArrayList<>();
itemListProduct[n]= new ItemListProduct();
itemListProduct[n]= ds.getValue(ItemListProduct.class);
loca[counter].add(testHeaderlist.get(counter));
System.out.println(ds.child("item_NAME").getValue(String.class));
objectMap.put(loca[counter],itemListProduct[n]);
counter++;
}
Here the testHeaderlist
is an ArrayList<String>
where there are some string stored.这里的
testHeaderlist
是一个ArrayList<String>
,其中存储了一些字符串。 I wanted to store data in the below Image manner:我想以下面的图像方式存储数据:
So now my question is how can I retrieve the Key as well as the Object from "dataList".所以现在我的问题是如何从“dataList”中检索密钥以及 Object。 As from the code which I have shared at the TOP "n" number of list, and the object are stored in the dataList.
从我在列表的前“n”号共享的代码中,object 存储在 dataList 中。
The thing is that I want to retrieve to use it in ExpandableListView.问题是我想检索以在 ExpandableListView 中使用它。
loca
as header and itemListproduct
as my value Object. loca
作为 header 和itemListproduct
作为我的值 Object。 Where the both are stored at objectMap
.两者都存储在
objectMap
中。
Can anyone please solve it.任何人都可以请解决它。 Thank you!
谢谢!
It is permitted but rather atypical to have an ArrayList
as a key to a map.将
ArrayList
作为 map 的密钥是允许的,但不是典型的。 To get the Object
you need to do the following:要获得
Object
,您需要执行以下操作:
Object val = map.get(arrayList)
Here, arrayList
must contain the exact same strings in the same order as the ArrayList
key which refers to the desired object.在这里,
arrayList
必须包含与ArrayList
键相同的字符串,该键指的是所需的 object。
Example例子
Map<List<String>, Integer> map = new HashMap<>();
List<String> key = List.of("abc", "efg");
map.put(key, 20);
Integer v = map.get(List.of("efg","abc")); // different key so
// object not found
System.out.println(v); // prints null
v = map.get(List.of("abc", "efg"));
System.out.println(v); // prints 20
You can get all the Keys of a map by doing您可以通过以下方式获得 map 的所有密钥
Set<List<String>> set = map.keySet();
You also need to readup on HashMap and ArrayList to understand how they work.您还需要阅读 HashMap和ArrayList以了解它们的工作原理。 The following will keep replacing the object for the key of
list[0]
以下将继续替换 object 作为
list[0]
的键
dataList.put(list[0], object[0]);
dataList.put(list[0], object[1]);
dataList.put(list[0], object[2]);
dataList.put(list[0], object[3]);
When the above is done, list[0]
will only refer to object[3]
完成上述操作后,
list[0]
将只引用object[3]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.