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

我有一个这样的哈希图;


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

谢谢。


皈依舞
浏览 193回答 2
2回答

米脂

而不是使用 HashMap 使用 Multimap。并基于键获取 Collection 对象中的值。然后从每个 Collection 在所有列表中添加一个值。Multimap myList = ArrayListMultimap.create();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; myList.put("key","value1");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; myList.put("key","value2");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; myList.put("key","value3");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; myList.put("key","value4");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; myList.put("key1","value5");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; myList.put("key1","value6");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; myList.put("key1","value7");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; myList.put("key1","value8");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; myList.put("key2","value9");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; myList.put("key2","value10");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; myList.put("key2","value11");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; myList.put("key2","value12");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; myList.put("key3","value13");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; myList.put("key3","value14");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; myList.put("key3","value15");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; myList.put("key3","value16");然后从列表中获取所有密钥&nbsp; Set myKeySet = myList.keySet();&nbsp; Object[] keys = myKeySet.toArray();现在您已经设置了所有密钥。然后&nbsp; &nbsp; HashMap<String, String> listHashMap1 = new HashMap<>();&nbsp; &nbsp; HashMap<String, String> listHashMap2 = new HashMap<>();&nbsp; &nbsp; HashMap<String, String> listHashMap3 = new HashMap<>();&nbsp; &nbsp; HashMap<String, String> listHashMap4 = new HashMap<>();现在将值添加到每个列表&nbsp; for(int i=0;i<4;i++){&nbsp; &nbsp; &nbsp; &nbsp; List<String> keyval = (List)myList.get((String)keys[i]);&nbsp; &nbsp; &nbsp; &nbsp; listHashMap1.put((String)keys[i],keyval.get(0));&nbsp; &nbsp; &nbsp; &nbsp; listHashMap2.put((String)keys[i],keyval.get(1));&nbsp; &nbsp; &nbsp; &nbsp; listHashMap3.put((String)keys[i],keyval.get(2));&nbsp; &nbsp; &nbsp; &nbsp; listHashMap4.put((String)keys[i],keyval.get(3));&nbsp; &nbsp; &nbsp;}现在您已经设置了四行列表。这个 Multimap 使用的是谷歌番石榴。所以你需要在你的 gradle 中实现番石榴,如下所示: implementation 'com.google.guava:guava:26.0-jre'

慕的地10843

从您提供的代码看来,源hashMap的类型是Map<String, String[]>。您可以遍历地图的条目,并为每个条目将四个值存储在不同的结果容器中。由于您指出结果容器也应该是地图:&nbsp; &nbsp; Map<String, String> one = new HashMap<>();&nbsp; &nbsp; Map<String, String> two = new HashMap<>();&nbsp; &nbsp; Map<String, String> three = new HashMap<>();&nbsp; &nbsp; Map<String, String> four = new HashMap<>();&nbsp; &nbsp; hashMap.entrySet().forEach(entry -> {&nbsp; &nbsp; &nbsp; &nbsp; String[] values = entry.getValue();&nbsp; &nbsp; &nbsp; &nbsp; one.put(entry.getKey(), values[0]);&nbsp; &nbsp; &nbsp; &nbsp; two.put(entry.getKey(), values[1]);&nbsp; &nbsp; &nbsp; &nbsp; three.put(entry.getKey(), values[2]);&nbsp; &nbsp; &nbsp; &nbsp; four.put(entry.getKey(), values[3]);&nbsp; &nbsp; });注意:假设每个键总是正好有四个值。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java