简体   繁体   English

如何遍历哈希图以获取每个第四个键,值对?

[英]How to iterate through a hashmap to get every fourth key, value pair?

I have a hashmap like this; 我有一个像这样的哈希表;

key value1
key value2
key value3
key value4
key1 value5
key1 value6
key1 value7
key1 value8
key2 value9
... and so on

Now I want to put this in a ListView which contains four rows , and in row1, I want: 现在,我要将其放入包含四行的ListView中,并在row1中,我想要:

key value1
key1 value5
key2 value9
key3 value13 and so on till end

And in row2, I want: 在第二行中,我想要:

key value2
key1 value6
key2 value10
key3 value14 and so on till end

And then the same in rows 3 and 4. There are always only 4 rows. 然后在第3行和第4行中相同。总是只有4行。

I am having trouble iterating through this. 我在遍历此过程时遇到了麻烦。 Here is the code I wrote, but it doesn't work. 这是我写的代码,但是不起作用。

String[] fourrows = hashMap.get("KEY");
for (int i=0;i<fourrows.length;i++) {
HashMap<String, String> listHashMap = new HashMap<>();
listHashMap.put("TA",  "ROW VALUE "+fourrows[i]);

for (int j=1;j<hashMap.entrySet().size();j+=4) {
String val = hashMap.values().toArray()[j].toString();
String key = hashMap.get(hashMap.keySet().toArray()[0]).toString();

listHashMap.put("IA", key);
listHashMap.put("XA", val);
incmStmtList.add(listHashMap);
}
}

//Then I pass TA, IA and XA to a simple list adapter and add it to a listView.
incmAdapter = new SimpleAdapter(getContext(), incmStmtList,R.layout.content_results, new String[]{"TA", "IA", "XA"},new int[]{R.id.ta, R.id.ia, R.id.tota});
listView.setVerticalScrollBarEnabled(true);
listView.setAdapter(incmAdapter);

Thanks. 谢谢。

Instead of using HashMap use Multimap. 不要使用HashMap,而应使用Multimap。 And based on key get the value in Collection object. 并基于键获取Collection对象中的值。 then from each Collection add one one value on all the list. 然后从每个Collection中在所有列表中添加一个值。

Multimap myList = ArrayListMultimap.create(); Multimap myList = ArrayListMultimap.create();

            myList.put("key","value1");
            myList.put("key","value2");
            myList.put("key","value3");
            myList.put("key","value4");
            myList.put("key1","value5");
            myList.put("key1","value6");
            myList.put("key1","value7");
            myList.put("key1","value8");
            myList.put("key2","value9");
            myList.put("key2","value10");
            myList.put("key2","value11");
            myList.put("key2","value12");
            myList.put("key3","value13");
            myList.put("key3","value14");
            myList.put("key3","value15");
            myList.put("key3","value16");

Then get all keys from list 然后从列表中获取所有密钥

  Set myKeySet = myList.keySet();
  Object[] keys = myKeySet.toArray();

Now you have set of all key. 现在您已经设置了所有密钥。 then 然后

    HashMap<String, String> listHashMap1 = new HashMap<>();
    HashMap<String, String> listHashMap2 = new HashMap<>();
    HashMap<String, String> listHashMap3 = new HashMap<>();
    HashMap<String, String> listHashMap4 = new HashMap<>();

Now add the value to every list 现在将值添加到每个列表

  for(int i=0;i<4;i++){
        List<String> keyval = (List)myList.get((String)keys[i]);
        listHashMap1.put((String)keys[i],keyval.get(0));
        listHashMap2.put((String)keys[i],keyval.get(1));
        listHashMap3.put((String)keys[i],keyval.get(2));
        listHashMap4.put((String)keys[i],keyval.get(3));
     }

Now you have set of four row list. 现在,您已设置了四行列表。 This Multimap is using google guava. 此Multimap使用的是Google番石榴。 So you need to implement guava in your gradle as below: implementation 'com.google.guava:guava:26.0-jre' 因此,您需要在gradle中实现如下所示的guava:实现'com.google.guava:guava:26.0-jre'

From the code you supplied it seems that the source hashMap is of type Map<String, String[]> . 从您提供的代码看来,源hashMap的类型为Map<String, String[]>

You could iterate over the entries of the map, and for each entry store the four values in different result containers. 您可以遍历地图的条目,对于每个条目,将四个值存储在不同的结果容器中。 Since you indicated the result containers should be maps as well: 由于您已指出结果容器也应为地图:

    Map<String, String> one = new HashMap<>();
    Map<String, String> two = new HashMap<>();
    Map<String, String> three = new HashMap<>();
    Map<String, String> four = new HashMap<>();

    hashMap.entrySet().forEach(entry -> {
        String[] values = entry.getValue();
        one.put(entry.getKey(), values[0]);
        two.put(entry.getKey(), values[1]);
        three.put(entry.getKey(), values[2]);
        four.put(entry.getKey(), values[3]);
    });

Note: assuming every key always has exactly four values. 注意:假设每个键始终具有正好四个值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM