java中如何使用map存取数据

如题所述

java中使用map存取数据的方法如下:

1、需要指定其中的K,V;k=keyv=value。

2、指定其中K、V的类型。

3、接下来往map中添加数据。

4、需要注意的是,如果map中已经存在的key,后面添加的会覆盖掉当前值。

接下来对map进行遍历输出。可以看到其中a的值已经被覆盖,此时就已经使用map存储好数据了。

温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2017-11-28
1.声明一个map: Map map = new HashMap();
2.向map中放值,注意:map是key-value的形式存放的.如:

map.put(”sa”,”dd”);

3.从map中取值:String str = map.get(”sa”).toString();结果是:str = ”dd”;
4.遍历一个map,从中取得key 和value
Map map = new HashMap() ;

Iterator it = map.entrySet().iterator() ;
while (it.hasNext())
{
Map.Entry entry = (Map.Entry) it.next() ;
Object key = entry.getKey() ;
Object value = entry.getValue() ;
}本回答被网友采纳
第2个回答  2009-11-17
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
* Map
* -- HashMap
* -特点: 1、可以使用NULL值 和NULL键
* 2、不同步
* (除了非同步和允许使用NULL,其他与HashTable 没什么区别)
* -方法
* 如下
* @author caihai
*
*/
public class HashMapDemo {
public static void main(String args[])
{
System.out.println("HashMap:----------------------------------");
Map<String,Integer> hashmap=new HashMap<String,Integer>();
//按键-值的方式 存入数据
hashmap.put("1", 1);
hashmap.put("2",2);
hashmap.put("4",4);
hashmap.put("3",3);
hashmap.put(null,null);
//containsKey
System.out.println("判断是否含有”1“此键"+hashmap.containsKey("1"));
System.out.println("-------------------------------------------");
//containsValue
System.out.println("判断时候含有”1“此值"+hashmap.containsValue(1));
System.out.println("-------------------------------------------");
//遍历MAP 的二种方法
//keySet
System.out.println("利用keyset方式 遍历MAP");
Set<String> keyset=hashmap.keySet();
for(String ks:keyset)
{
System.out.println("keyset---key:"+ks+" value:"+hashmap.get(ks));
}
System.out.println("-------------------------------------------");
System.out.println("利用entrySet方式 遍历MAP");
Set<Map.Entry<String, Integer>> entryset=hashmap.entrySet();
for(Map.Entry<String,Integer> entry:entryset)
{
System.out.println("entryset---key:"+entry.getKey()+" value:"+entry.getValue());
}
System.out.println("-------------------------------------------");
System.out.println("判断Hashmap是否为空"+hashmap.isEmpty());
System.out.println("-------------------------------------------");
System.out.println("通过get(Object key)获得对应值"+hashmap.get(null));
System.out.println("-------------------------------------------");
System.out.println("计算Map的大小"+hashmap.size());
Map<String,Integer> insertmap=new HashMap<String,Integer>();
insertmap.put("100",100);
insertmap.put("101",101);
insertmap.put("102",102);
System.out.println("-------------------------------------------");
System.out.println("将MAP加入到MAP中");
hashmap.putAll(insertmap);
Set<String> keyseti=hashmap.keySet();
for(String ks:keyseti)
{
System.out.println("key:"+ks+" value:"+hashmap.get(ks));
}
System.out.println("-------------------------------------------");
System.out.println("Get the Map values ,return Collection:");
Collection<Integer> values=hashmap.values();
Iterator<Integer> it=values.iterator();
while(it.hasNext())
{
System.out.println("The value: "+it.next());
}
}
}
第3个回答  2009-11-16
通过键值对的方式进行存取数据的
相似回答