如何解决如何从HashMap列表中获取值?
| 我有一个HashMap
\的List
,其中键的类型为Integer
,值的类型为Long
。
List<HashMap<Integer,Long>> listofHash = new ArrayList<HashMap<Integer,Long>>();
for (int i = 0; i < 10; i++) {
HashMap<Integer,Long> mMap = new HashMap<Integer,Long>();
mMap.put(Integer.valueOf(i),Long.valueOf(100000000000L+i));
listofHash.add(mMap);
}
现在,如何从from1ѭ列表中检索键和值?
如果使用Collection
类是解决方案,请告诉我。
更新1:
实际上,我正在从数据库中获取值并将其放入HashMap中
public static List<Map<Integer,Long>> populateMyHashMapFromDB()
{
List<HashMap<Integer,Long>>();
for (int i = 0; i < 10; i++) {
HashMap<Integer,Long>();
mMap.put(Integer.valueOf(getIntFromDB(i)),Long.valueOf(getLongFromDB(i)));
listofHash.add(mMap);
}
return listofHash;
}
getIntFromDB和getLongFromDB可以分别检索任何int和long值。
现在这是我要从DB获取键和值的值的地方
public void getmyDataBaseValues()
{
List<HashMap<Integer,Long>> listofHash=populateMyHashMapFromDB();
// This is where i got confused
}
这就是彼得·劳瑞(Peter Lawrey)建议的原因
public class HashMaps {
public static void main(String[] args) {
// Todo Auto-generated method stub
testPrintHashmapValues(putToHashMap());
testPrintHashmapValuesWithList(putToHashMapWithList());
}
public static Map<Integer,Long> putToHashMap() {
Map<Integer,Long> map = new LinkedHashMap<Integer,Long>();
for (int i = 0; i < 10; i++) {
map.put(Integer.valueOf(i),Long.valueOf(200000000000L + i));
}
return map;
}
public static void testPrintHashmapValues(Map<Integer,Long> map) {
for (Map.Entry<Integer,Long> entry : map.entrySet()) {
System.out.println(\"key: \" + entry.getKey() + \" value: \"
+ entry.getValue());
}
}
public static List<Map<Integer,Long>> putToHashMapWithList() {
List<Map<Integer,Long>> listofHash = new ArrayList<Map<Integer,Long>>();
for (int i = 0; i < 10; i++) {
Map<Integer,Long>();
mMap.put(Integer.valueOf(i),Long.valueOf(100000000000L + i));
listofHash.add(mMap);
}
return listofHash;
}
public static void testPrintHashmapValuesWithList(
List<Map<Integer,Long>> listofHash) {
for (int i = 0; i < 10; i++) {
Map<Integer,Long> map = listofHash.get(i);
for (Map.Entry<Integer,Long> entry : map.entrySet()) {
System.out.println(i + \" hashMap\");
System.out.println(\"key: \" + entry.getKey() + \" value: \"
+ entry.getValue());
}
}
}
}
即使没有创建列表,我的任务也已完成。
解决方法
我仍然不清楚为什么要清单。
public static Map<Integer,Long> populateMyHashMapFromDB() {
Map<Integer,Long> map = new LinkedHashMap<Integer,Long>();
for (int i = 0; i < 10; i++)
map.put(getIntFromDB(i),getLongFromDB(i));
return map;
}
Map<Integer,Long> map = populateMyHashMapFromDB();
Long value = map.get(key);
此集合并非旨在为您轻松提供键/值对。如果您需要此功能,建议使用其他结构。
假设您有一些错误的代码无法更改,则可以执行
List<Map<Integer,Long>> maps = new ArrayList<Map<Integer,Long>>();
Map<Integer,Long> all = new HashMap<Integer,Long>();
for(Map<Integer,Long> map: maps)
all.putAll(map);
for(Map.Entry<Integer,Long> entry: all.entrySet() {
// do something which each key/value.
}
在此示例中,您不需要列表或地图。
long[] longs = new long[10];
for (int i = 0; i < 10; i++)
longs[i] = i;
int key = 1;
int num = longs[key];
, 您遍历列表以获取地图,然后遍历其键集:
public static void main(String[] args) throws NoSuchAlgorithmException {
List<Map<Integer,Long>> ListofHash = new ArrayList<Map<Integer,Long>>();
for (int i = 0; i < 10; i++) {
Map<Integer,Long> mMap = new HashMap<Integer,Long>();
mMap.put(Integer.valueOf(i),Long.valueOf(100000000000L + i));
ListofHash.add(mMap);
}
for (Map<Integer,Long> map : ListofHash) {
for (Integer key : map.keySet()) {
System.out.println(map.get(key));
}
}
}
注意:我还更改了一些代码,在可能的情况下使用Map
代替HashMap
, 我假设您在问如何从ArrayList中获取它,
ListofHash.get(0).get(Object key);
对象键表示您想要的任何整数键
祝好运!
, 这种效果:
public static Long getValue(Integer key)) {
for (HashMap<Integer,Long> entry : ListofHash) {
if (entry.containsKey(key)) {
return entry.get(key);
}
}
return null;
}
//Retrieve all key/value
for (HashMap<Integer,Long> entry : ListofHash) {
for (Integer key : entry.keySet()) {
System.out.println(\"Key : \" + key + \",value: \" + entry.get(key));
}
}
PS未经测试。
, List<HashMap<Integer,Long>> ListofHash = new ArrayList<HashMap<Integer,Long>>();
for (int i = 0; i < 10; i++) {
HashMap<Integer,Long>();
mMap.put(Integer.valueOf(i),Long.valueOf(100000000000L+i));
ListofHash.add(mMap);
}
, 您总共有10张地图,仅存储了一个元素。这些地图存储在列表中。您可以执行以下操作检索这些Map及其元素:
public void testPrintHashmapValues() {
List<HashMap<Integer,Long>> listOfHash = new ArrayList<HashMap<Integer,Long>>();
for (int i = 0; i < 10; i++) {
HashMap<Integer,Long>();
mMap.put(Integer.valueOf(i),Long.valueOf(100000000000L+i));
listOfHash.add(mMap);
}
for (int i = 0; i < 10; i++) {
Map<Integer,Long> map = listOfHash.get(i);
for (Map.Entry<Integer,Long> entry : map.entrySet()) {
System.out.println(i + \" hashMap\");
System.out.println(\"key: \" + entry.getKey() + \" value: \" + entry.getValue());
}
}
}
此外,变量不应以大写字母(ListOfHash)开头。即使Java不会抱怨,这也是一种不好的做法,并且很难读取这样的代码,因为人们可能会认为它是类名而不是变量。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。