如何从HashMap中的列表获取值? [英] How to Get Values from List of HashMap?
问题描述
我有一个列表
的的HashMap
的有型的键整数
和类型的值龙
。
列表<&HashMap的LT;为Integer,Long>> ListofHash =新的ArrayList<&HashMap的LT;为Integer,Long>>();
对(INT I = 0; I&小于10;我++){
HashMap的<为Integer,Long> MMAP =新的HashMap<为Integer,Long>();
mMap.put(Integer.valueOf(i)中,Long.valueOf(100000000000L + I));
ListofHash.add(MMAP);
}
现在,我该如何检索的HashMap
?
如果使用收藏
类是解决方案,请微启我。
更新1:
其实我正从数据库中的值,并把该进一个HashMap
公共静态列表<地图<为Integer,Long>> populateMyHashMapFromDB()
{清单<&HashMap的LT;为Integer,Long>> ListofHash =新的ArrayList<&HashMap的LT;为Integer,Long>>();对(INT I = 0; I&小于10;我++){
HashMap的<为Integer,Long> MMAP =新的HashMap<为Integer,Long>();
mMap.put(Integer.valueOf(getIntFromDB(I)),Long.valueOf(getLongFromDB(I)));
ListofHash.add(MMAP);
}
返回ListofHash;
}
在这里getIntFromDB和getLongFromDB可以分别retreive任何int和long值。</ P>
现在,这是我想要得到我的价值观,我从DB键和值
得到 公共无效getmyDataBaseValues()
{
清单&LT;&HashMap的LT;为Integer,Long&GT;&GT; ListofHash = populateMyHashMapFromDB();//这是我弄糊涂了}
答案的这就是为什么彼得Lawrey 的建议
公共类HashMaps这样{
公共静态无效的主要(字串[] args){
// TODO自动生成方法存根 testPrintHashmapValues(putToHashMap());
testPrintHashmapValuesWithList(putToHashMapWithList());
} 公共静态地图&LT;为Integer,Long&GT; putToHashMap(){
地图&LT;为Integer,Long&GT;地图=新的LinkedHashMap&LT;为Integer,Long&GT;();
对(INT I = 0; I&小于10;我++){
map.put(Integer.valueOf(i)中,Long.valueOf(200000000000L + I));
}
返回地图;
} 公共静态无效testPrintHashmapValues(地图&LT;为Integer,Long&GT;地图){
对于(Map.Entry的&LT;为Integer,Long&GT;项:map.entrySet()){
的System.out.println(键+ entry.getKey()+值
+ entry.getValue());
}
} 公共静态列表与LT;地图&LT;为Integer,Long&GT;&GT; putToHashMapWithList(){
清单&LT;地图&LT;为Integer,Long&GT;&GT; listOfHash =新的ArrayList&LT;地图&LT;为Integer,Long&GT;&GT;();
对(INT I = 0; I&小于10;我++){
地图&LT;为Integer,Long&GT; MMAP =新的HashMap&LT;为Integer,Long&GT;(); mMap.put(Integer.valueOf(i)中,Long.valueOf(100000000000L + I));
listOfHash.add(MMAP);
}
返回listOfHash;
} 公共静态无效testPrintHashmapValuesWithList(
清单&LT;地图&LT;为Integer,Long&GT;&GT; listOfHash){ 对(INT I = 0; I&小于10;我++){
地图&LT;为Integer,Long&GT;地图= listOfHash.get(ⅰ);
对于(Map.Entry的&LT;为Integer,Long&GT;项:map.entrySet()){
的System.out.println(1 +的HashMap);
的System.out.println(键+ entry.getKey()+值
+ entry.getValue());
}
}
}}
我的任务就是没有建立一个列表就算做完了。
它仍然不清楚我为什么你想有一个列表。
公共静态地图&LT;为Integer,Long&GT; populateMyHashMapFromDB(){
地图&LT;为Integer,Long&GT;地图=新的LinkedHashMap&LT;为Integer,Long&GT;();
的for(int i = 0;我小于10;我++)
map.put(getIntFromDB(ⅰ),getLongFromDB(ⅰ));
返回地图;
}地图&LT;为Integer,Long&GT;地图= populateMyHashMapFromDB();
Long值= map.get(键);
这集的目的不是给你的键/值很容易对。我fyou需要此功能,我建议使用不同的结构。
假设你有一个不好的一些code你不能改变,你可以做
列表与LT;地图&LT;为Integer,Long&GT;&GT;地图=新的ArrayList&LT;地图&LT;为Integer,Long&GT;&GT;();地图&LT;为Integer,Long&GT;所有=新的HashMap&LT;为Integer,Long&GT;();
对于(地图&LT;为Integer,Long&GT;地图:地图)
all.putAll(地图);对于(Map.Entry的&LT;为Integer,Long&GT;项:all.entrySet(){
//做一些事情,每个键/值。
}
在这个例子中,你并不需要一个列表或地图。
长[] =渴望新长[10];
的for(int i = 0;我小于10;我++)
多头[我] =我;INT键= 1;
INT NUM =多头[关键]
I have a List
of HashMap
's which has key of type Integer
and value of type 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);
}
Now, how do I retrieve the key and value from the list of HashMap
?
If using Collection
class is the solution, please enlight me.
Update 1:
Actually i am getting the value from the database and putting that into a HashMap
public static List<Map<Integer, Long>> populateMyHashMapFromDB()
{
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(getIntFromDB(i)), Long.valueOf(getLongFromDB(i)));
ListofHash.add(mMap);
}
return ListofHash;
}
where getIntFromDB and getLongFromDB can retreive any int and long values respectively.
Now this is where i want to get my values that i got from DB both keys and values
public void getmyDataBaseValues()
{
List<HashMap<Integer,Long>> ListofHash=populateMyHashMapFromDB();
// This is where i got confused
}
ANSWER This is why Peter Lawrey suggested
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 = new HashMap<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());
}
}
}
}
My Task is done even without creating a List.
Its still not clear to me why you want a list.
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);
This collection isn't designed to give you the key/values pairs easily. I fyou need to this functionality I would suggest using a different structure.
Assuming you have a some bad code you cannot change, you can do
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.
}
In this example you don't need a List or a Map.
long[] longs = new long[10];
for (int i = 0; i < 10; i++)
longs[i] = i;
int key = 1;
int num = longs[key];
这篇关于如何从HashMap中的列表获取值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!