zoukankan      html  css  js  c++  java
  • ava:Map借口及其子类HashMap三

    ava:Map借口及其子类HashMap三

    HashMap常用子类(异步非安全线程,性能高; Hashtable:同步的安全线程,性能低)

    map(HashMap)中的key,value可以通过 Set<E>,或者Conllection<E>来接收.

    Map<String,Integer> allMap = new HashMap<String, Integer>();
    		allMap.put("zhangsan", 1);
    		allMap.put("zhangsan", 2);
    		allMap.put("lisi", 3);
    		allMap.put("tianqi", 4);
    		
    		//Integer value = allMap.get("zhangsan");
    		//System.out.println(value);
    		
    		Collection<Integer> keys = allMap.values();
    		Iterator<Integer> iter = keys.iterator();
    		while(iter.hasNext())
    		{
    			Integer str = iter.next();
    			System.out.println( str + "、" );
    		}
    		
    

      

    结果:

    3、
    2、
    4、
    

      

    注意事项:

    Map不能直接使用Iterator类输出

    在集合的标准操作中所有的集合内容最好使用Iterator进行输出,但在Map接口中并没有明确的定义出这样的操作。如果没有的话,则必须深入了解Map的机制。

    在Map中虽然是以一对值得形式出现的,可是真正的保存的还是一个单独的对象,即:程序key->alue的存放在一个对象之中,之后将对象加入到集合里。

    Map.Entry,Map实体,从定义格式上可以发现,此接口属于STATIC静态声明的接口。而且是一个内部接口。

    对于Map和Map.Entry的关系,如下图:

    MAP.Entry [ 一组对象数据]

    Map.Entry [ 一组对象数据]

    Map.Entry [一组对象数据]

    .......                                         <-------------------- 增加元素 Map.Entry[ key=>value 一组对象数据]

    所以:下面就可以给出Map接口使用Iterator输出的标准操作:

    1.通过Map接口中的:Set<Map.Entry<K, V>> entrySet()方法取得Set集合

    2.通过Set接口,为Iterator进行初始化操作

    3.通过Iterator取得每一个Map.Entry

    4.通过Map.Entry将KEY与VALUE分离。

    例子:

    Map<String, Integer> allSet = new HashMap<String, Integer>();
    		allSet.put("zhangsan", 1);
    		allSet.put("zhangsan", 2);
    		allSet.put("lisi", 3);
    		allSet.put("wangwu", 4);
    		
    		Set<Map.Entry<String,Integer>> allList = allSet.entrySet();
    		Iterator<Map.Entry<String,Integer>> iter = allList.iterator();
    		while(iter.hasNext())
    		{
    			Map.Entry<String, Integer> map = iter.next();
    			System.out.println( map.getKey() + "-->" + map.getValue()  );
    			
    		}
    

      

    Set<Map.Entry<String,Integer>> allList = allSet.entrySet();
    Iterator<Map.Entry<String,Integer>> iter = allList.iterator();
    while(iter.hasNext())
    {
    	Map.Entry<String, Integer> map = iter.next();
    	System.out.println( map.getKey() + "-->" + map.getValue()  );
    			
    }

    结果:

    lisi-->3
    zhangsan-->2
    wangwu-->4
    

      

    或者Foreach循环:

    	Map<String, Integer> allSet = new HashMap<String, Integer>();
    		allSet.put("zhangsan", 1);
    		allSet.put("zhangsan", 2);
    		allSet.put("lisi", 3);
    		allSet.put("wangwu", 4);
    		
    
    		//或者
    		for(Map.Entry<String, Integer> map: allSet.entrySet())
    		{
    			System.out.println( map.getKey() + "-->" + map.getValue());
    		}
    

      

  • 相关阅读:
    转Asktom:Pipelined Functions
    转:PVE法师附魔
    附魔300375图纸掉落大全
    转:Oracle直方图详解
    转:JSON的序列化及GET异步调用.
    转:ORACLE 中dbms_stats的使用
    jQuery中$.each的用法
    魔兽世界天赋详解之 法师篇 一冰法
    Miley's Oracle讲堂第三课:如何在Oracle中使用对象表存储数据.
    台服体验之急速升级
  • 原文地址:https://www.cnblogs.com/achengmu/p/7502139.html
Copyright © 2011-2022 走看看