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

时间:2018-11-05 02:13:16

标签: java android loops listview hashmap

我有一个像这样的哈希图;

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

现在,我要将其放入包含四行的ListView中,并在row1中,我想要:

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

在第2行中,我想要:

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

然后在第3行和第4行中相同。总是只有4行。

在遍历此过程时遇到麻烦。这是我写的代码,但是不起作用。

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);

谢谢。

2 个答案:

答案 0 :(得分:0)

使用Multimap代替使用HashMap。并基于键获取Collection对象中的值。然后在每个集合中的所有列表中添加一个值。

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");

然后从列表中获取所有密钥

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

现在您已经设置了所有键。然后

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

现在将值添加到每个列表中

  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));
     }

现在,您已设置了四行列表。此Multimap使用的是Google番石榴。  因此,您需要在gradle中实现如下的番石榴:  实施'com.google.guava:guava:26.0-jre'

答案 1 :(得分:0)

根据您提供的代码,看来源hashMap的类型为Map<String, String[]>

您可以遍历地图的条目,并为每个条目将四个值存储在不同的结果容器中。由于您指出结果容器也应该是地图:

    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]);
    });

注意:假设每个键始终具有正好四个值。