简体   繁体   English

如何从 Map 中检索值<arraylist<string> , 对象&gt; </arraylist<string>

[英]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.您还需要阅读 HashMapArrayList以了解它们的工作原理。 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.

相关问题 如何安全地从 Map 中检索 ArrayList<String, Object> - How to safely retrieve ArrayList from Map<String, Object> 如何从ArrayList检索值 <String> 如果后者是Map中的Key? - How to retrieve values from ArrayList<String> if the latter is a Key in Map? 如何从java地图中获取价值:地图 <String,ArrayList<String> &gt;? - How to get value from the java map : Map<String,ArrayList<String>>? 如何根据对象从地图中检索值 - How to retrieve value from a map based on object 如何从 ArrayList 中检索特定值<string>在 Spring</string> - How do I retrieve particular value from an ArrayList<String> in Spring 如何填充ArrayList <String> 从ArrayList <Object> - How to populate an ArrayList<String> from ArrayList<Object> 如何将arrayList存储为Map&#39;string作为键以及如何检索它。 - how to store a arrayList to Map 'string as key and how to retrieve it..! 如何从地图中获取价值? - how to retrieve value from Map? 如何理解ArrayList <Map<String,Object> &gt; 在多层循环中,ArrayList <Map<String,Object> &gt; 内容一样吗? - How to understand the ArrayList<Map<String,Object>> in multi-layer loop, the ArrayList<Map<String,Object>> contents are the same? 我如何从地图中获取价值 <Object,List<Object> &gt; obj = new HashMap <Object,List<Object> &gt;(); - How can i retrieve value from Map<Object,List<Object>> obj=new HashMap<Object,List<Object>>();
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM