Java基础--集合框架之Map


凌风博客原创作品。转载请注明出处:http://blog.csdn.net/q549130180/article/details/45313467

Map集合:该集合存储键值对,一对一对往里存,而且要保证键的唯一性

1.添加
put(K key, Vvalue)
putAll(Map<? extends K,? extends V> m)


2.删除
clear()
remove(Object key)


3.判断
containsValue(Object value)
containsKey(Object key)
isEmpty()


4.获取
get(Object key)
size()
values()


entrySet()
KeySet()


Map
|--Hashtable:底层是哈希表数据结构,不可以存入null键null值。该集合是线程同步的。JDK1.0 ,效率低
|--HashMap:底层的哈希表数据结构,允许使用null值和null键,该集合是不同步的。JDK1.2 ,效率高
|--TreeMap:底层是二叉树数据结构,线程不同步,可以用于给Map集合中的键进行排序


和Set很像,其实,Set底层就是使用了Map集合

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. import java.util.*;  
  2. class MapDemo1  
  3. {  
  4.     public static void main(String[] args)   
  5.     {  
  6.         Map<String,String> map = new HashMap<String,String>();  
  7.   
  8.         //添加元素,如果出现添加时,相同的键,那么后添加的值会覆盖原有键对应值  
  9.         //并且put方法会返回被覆盖的值  
  10.         System.out.println("put:"+map.put("01","zhangsan1"));  
  11.         System.out.println("put:"+map.put("01","zhangsan1"));  
  12.         map.put("02","zhangsan2");  
  13.         map.put("03","zhangsan3");  
  14.   
  15.         System.out.println("containsKey"+map.containsKey("022"));  
  16.         //System.out.println("remove:"+map.remove("02"));  
  17.   
  18.         System.out.println("get:"+map.get("023"));  
  19.   
  20.         map.put("04",null);  
  21.         System.out.println("get:"+map.get("04"));  
  22.         //可以通过get方法的返回值来判断一个键是否存在,to通过返回null来判断  
  23.   
  24.         //获取map集合中所以的值  
  25.         Collection<String> coll = map.values();  
  26.   
  27.         System.out.println(coll);  
  28.         System.out.println(map);  
  29.   
  30.     }  
  31. }  

Map集合的两种取出方式:
1.Set<K> KeySet:将Map中所有的键存入到Set集合,因为Set具备迭代器
所以可以用迭代方式取出所有的键,在根据get方法,获取每一个键对应的值


Map集合的取出原理:将map集合转成Set集合,再通过迭代器取出


2.Set<Map.Entry<K,V>> entrySet:将map集合中的映射关系存入到了Set集合中
而这个关系的数据类型就是:Map.Entry

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. import java.util.*;  
  2. class MapDemo2  
  3. {  
  4.     public static void main(String[] args)   
  5.     {  
  6.         Map<String,String> map = new HashMap<String,String>();  
  7.   
  8.         map.put("02","zhangsan2");  
  9.         map.put("03","zhangsan3");  
  10.         map.put("01","zhangsan1");  
  11.         map.put("04","zhangsan4");  
  12.   
  13.         //将Map集合中的映射关系取出,存入到Set集合中  
  14.         Set<Map.Entry<String,String>> entrySet = map.entrySet();  
  15.   
  16.         Iterator<Map.Entry<String,String>> it = entrySet.iterator();  
  17.           
  18.         while (it.hasNext())  
  19.         {  
  20.             Map.Entry<String,String> me = it.next();  
  21.             String key = me.getKey();  
  22.             String value = me.getValue();  
  23.   
  24.             System.out.println(key+"--"+value);  
  25.         }  
  26.   
  27.   
  28.         /* 
  29.         //KeySet演示 
  30.         //先获取map集合的所有键的Set集合,KeySet() 
  31.         Set<String> KeySet = map.KeySet(); 
  32.  
  33.         //有了Set集合,就可以获取其迭代器 
  34.         Iterator<String> it = KeySet.iterator(); 
  35.  
  36.         while (it.hasNext()) 
  37.         { 
  38.             String key = it.next(); 
  39.             //有了键可以通过map集合的get方法获取其对应的值 
  40.             String value = map.get(key); 
  41.             System.out.println("key:"+key+",value:"+value); 
  42.         } 
  43.         */  
  44.           
  45.     }  
  46. }  

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. Map.Entry说明:Map.Entry其实Entry也是一个接口,它是Map接口中的一个内部接口  
  2.   
  3. interface Map  
  4. {  
  5.     public static interface Entry  
  6.     {  
  7.         public abstract Object getKey();  
  8.         public abstract Object getValue();  
  9.     }  
  10. }  
  11.   
  12. class HashMap implements Map  
  13. {  
  14.     class Haha implements Map.Entry  
  15.     {  
  16.         public abstract Object getKey(){}  
  17.         public abstract Object getValue(){}  
  18.     }  
  19. }  

练习:
"jsdfjfdjfhvbbsdd"获取该字符串中的字母出现的次数


希望打印结果:a(1)c(2)......


通过结果发现,每一个字母都有对应的次数
说明字母和次数之间都有映射关系


注意了,当发现有映射关系时,可以选择Map集合
因为Map集合中存放就是映射关系


什么时候使用Map集合呢?
当数据之间存放着映射关系时,就要先想Map集合


思路
1.将字符串转换成字符数组,因为要对每一个字母进行操作


2.定义一个Map集合,因为打印结果的字母有顺序,,所以使用TreeMap集合


3.遍历字符数组
将每一个字母作为键去查Map集合
如果返回null,将该字母和1存入到Map集合中
如果返回不是null,说明该字母在Map集合已经存在并有对应次数
那么就获取该次数并进行自增,然后将该字母和自增后的次数存入到Map集合中,覆盖调用原来键所对应的值


4.将Map集合中的数据变成指定的字符串形式返回。

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. import java.util.*;  
  2. class MapTest3  
  3. {  
  4.     public static void main(String[] args)   
  5.     {  
  6.         String s = charCount("abcdjskifkabcnxbdhfjghjdhfjsdfjhf");  
  7.         System.out.println(s);  
  8.     }  
  9.   
  10.     public static String charCount(String str)  
  11.     {  
  12.         char[] chs = str.toCharArray();  
  13.   
  14.         TreeMap<Character,Integer> tm = new TreeMap<Character,Integer>();  
  15.           
  16.         int count = 0;  
  17.         for (int x=0;x<chs.length ;x++ )  
  18.         {  
  19.             if(!(chs[x]>='a' && chs[x]<='z' || chs[x]>='A' && chs[x]<='Z'))  
  20.                 continue;  
  21.   
  22.             Integer value = tm.get(chs[x]);  
  23.   
  24.             if (!(value==null))  
  25.                     count = value;  
  26.             count++;  
  27.             tm.put(chs[x],count);  
  28.             count = 0;  
  29.             /* 
  30.             if (value==null) 
  31.             { 
  32.                 tm.put(chs[x],1); 
  33.             }else 
  34.             { 
  35.                 value = value +1; 
  36.                 tm.put(chs{x],value); 
  37.             } 
  38.             */  
  39.   
  40.   
  41.         }  
  42.   
  43.         StringBuilder sb = new StringBuilder();  
  44.           
  45.         Set<Map.Entry<Character,Integer>> entrySet = tm.entrySet();  
  46.         Iterator<Map.Entry<Character,Integer>> it = entrySet.iterator();  
  47.           
  48.         while (it.hasNext())  
  49.         {  
  50.             Map.Entry<Character,Integer> me = it.next();  
  51.             Character ch = me.getKey();  
  52.             Integer value = me.getValue();  
  53.   
  54.             sb.append(ch+"("+value+")");  
  55.         }  
  56.   
  57.         return sb.toString();  
  58.   
  59.   
  60.     }  
  61. }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值