无线&移动互联网技术研发

换位思考·····
posts - 19, comments - 53, trackbacks - 0, articles - 283
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

Map 遍历

Posted on 2009-12-22 09:21 Gavin.lee 阅读(206) 评论(0)  编辑  收藏 所属分类: java SE & EE
package com.Gavin.tools.util;

import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**  
 * 
@author Gavin.lee  
 * @date 2009-05-04 11:45am
 * 
@param <K>  
 * 
@param <V>  
 
*/

public class MapUtil<K, V> {
    
    
public void filter1(Map map) {
        Iterator it 
= map.entrySet().iterator();
        
while (it.hasNext()) {
            Map.Entry me 
= (Map.Entry) it.next();
            System.out.println(
"key:" + me.getKey() + ",value:" + me.getValue());
        }

        System.out.println(
"************************************filter1");
    }


    
public void filter2(Map map) {
        Hashtable
<String, String> ht = new Hashtable<String, String>();
        ht.put(
"Gavin""Kathy");
        ht.put(
"杨过""小龙女");
        
        
for (Map.Entry<String, String> me : ht.entrySet()) {
            System.out.println(
"key:" + me.getKey() + ",value:" + me.getValue());
        }

        System.out.println(
"************************************filter2");
    }


    
public void filter3(Map map) {
        Iterator it 
= map.keySet().iterator();
        
while (it.hasNext()) {
            String key;
            key 
= (String) it.next();
            System.out.println(
"key:" + key + ",value:" + map.get(key));
        }

        System.out.println(
"************************************filter3");
    }


    
public void filter4(Map map) {
        
for (Object o : map.keySet()) {
            System.out.println(
"key:" + o + ",value:" + map.get(o));
        }

        System.out.println(
"************************************filter4");
    }

    
    
/**  
     * 获得Map中特定value的key值     
     * 
@param map  
     * 
@param value  
     * 
@return  
     
*/

    
public K getMapKeyFromValue(Map<K, V> map, V value) {
        Set set 
= map.keySet();
        K key 
= null;
        Iterator it 
= set.iterator();
        
while (it.hasNext()) {
            key 
= (K) it.next();
            
if (value.equals(map.get(key))) {
                
return key;
            }

        }

        
return null;
    }


}


只有注册用户登录后才能发表评论。


网站导航: