1.保存形式
Key ---> value 的保存方式 键值对
2.常用子类
HashMap:无序存放,key不允许重复
HashTable: 无序存放,key不允许重复
2.1Map的使用方法
public class MapDemo01 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Map<String,String> map = new HashMap<String,String>();
map.put("key1","fjasmin");
map.put("key2","shlearry");
map.put("key3","anke");
map.put("key4","fangjian");
map.put("key5","hanxi");
map.put("key6","ye");
String str = map.get("key6");
System.out.println(str);
}
}
2.2 判断map中是否含有键值对
public class MapDemo2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Map<String,String> map = new HashMap<String,String>();
map.put("key1","fjasmin");
map.put("key2","shlearry");
map.put("key3","anke");
map.put("key4","fangjian");
map.put("key5","hanxi");
map.put("key6","ye");
/*
* String str = map.get("key6"); System.out.println(str);
*/
if(map.containsKey("key1")) {
System.out.println("key keep!");
}else {
System.out.println("key lost!");
}
if(map.containsValue("fjasmin")) {
System.out.println("value keep!");
}else {
System.out.println("value lost!");
}
}
}
2.3遍历取出Map中的键值对
public class MapDemo3 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Map<String,String> map = new HashMap<String,String>();
map.put("key1","fjasmin");
map.put("key2","shlearry");
map.put("key3","anke");
map.put("key4","fangjian");
map.put("key5","hanxi");
map.put("key6","ye");
Set<String> s = map.keySet();
Iterator<String> iter = s.iterator();
while(iter.hasNext())
{ System.out.println(iter.next()); }
Collection<String> collection = map.values();
Iterator<String> iter1 = collection.iterator();
while(iter1.hasNext()) {
System.out.println(iter1.next());
}
}
}
网友评论