关于hashmap的一些操作

  1. 两个hashmap比对值然后把相同的值保存到list里

for(Entry<String, String> ENTRY:MAP.entrySet()){
					       
					           
					        for(Entry<String, String> entry:map.entrySet()){
					        	    if(ENTRY.getValue().equals(entry.getValue())) {
					        	    	List.add(ENTRY.getKey());
					        	    				}
					        		}
					              }
  • 关于hashmap去空value去掉重复value的操作

public static Map<String, String>  doThing(Map<String, String> map){
    Map<String, String> map2 = new HashMap<String, String>();
    Map<String, String> map3 = new HashMap<String, String>();
    //TreeMap:对map按key值排序
    TreeMap<String, String> treemap = new TreeMap<String, String>(map);
    Iterator<String> it = treemap.keySet().iterator();
    while (it.hasNext()) {
        String key = it.next();
        String value = treemap.get(key);
        if(map2.containsKey(value)){
            continue;
        }else{
            map2.put(value, value);
            map3.put(key, value);
        }
         
    }
    return map3;
}

public static void removeNullValue(Map map){ 
    Set set = map.keySet(); 
    for (Iterator iterator = set.iterator(); iterator.hasNext();) { 
        Object obj = (Object) iterator.next(); 
        Object value =(Object)map.get(obj); 
        remove(value, iterator); 
    } 
} 

private static void remove(Object obj,Iterator iterator){ 
    if(obj instanceof String){ 
        String str = (String)obj;
        if(isEmpty(str)){  //过滤掉为null和""的值 主函数输出结果map:{2=BB, 1=AA, 5=CC, 8=  }
//        if("".equals(str.trim())){  //过滤掉为null、""和" "的值 主函数输出结果map:{2=BB, 1=AA, 5=CC}
            iterator.remove(); 
        } 
       
    }else if(obj instanceof Collection){ 
        Collection col = (Collection)obj; 
        if(col==null||col.isEmpty()){ 
            iterator.remove(); 
        } 
         
    }else if(obj instanceof Map){ 
        Map temp = (Map)obj; 
        if(temp==null||temp.isEmpty()){ 
            iterator.remove(); 
        } 
         
    }else if(obj instanceof Object[]){ 
        Object[] array =(Object[])obj; 
        if(array==null||array.length<=0){ 
            iterator.remove(); 
        } 
    }else{ 
        if(obj==null){ 
            iterator.remove(); 
        } 
    } 
} 
 
public static boolean isEmpty(Object obj){
    return obj == null || obj.toString().length() == 0;
}


	//这个方法是去重
					 Map<String, String> map4 = new HashMap<String, String>();
				        map4 = doThing(map);
				        System.out.println("这里开始打印去掉重复值之后剩余的值");
				        for (String key : map4.keySet()){
				        	
				            System.out.println(key+":"+map4.get(key));
				        }
				        
				        //这个方法是去空
				        removeNullValue(map4);
				        //这里结束  
				        //这里开始打印测试结果
				        
				        Iterator iter = map4.keySet().iterator();
				        System.out.println("这里开始打印去除掉空值之后的值");
						while (iter.hasNext()) {
						String key = (String) iter.next();
						String val = map.get(key);
						
						System.out.println(key+","+val);
						}
						System.out.println("这里处理空值和重复值结束");
						//
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值