• java中两个map比较


        /**
         * 用map的keySet()的迭代器(性能效率较低)
         * 
         */
        public  void compareMap1 (){
     
            Map<String, String> m1 = new HashMap<String, String>();
            Map<String, String> m2 = new HashMap<String, String>();
     
            Iterator<String> iter1 = m1.keySet().iterator();
     
            while (iter1.hasNext()) {
                String m1Key = (String) iter1.next();
                if (!m1.get(m1Key).equals(m2.get(m1Key))) {//若两个map中相同key对应的value不相等
                    //......
                }
            }
     
        }

       /**
         * 用map的entrySet()的迭代器(性能效率较高)
         */
        public void compareMap2(){
            Map<String, String> m1 = new HashMap<String, String>();
            Map<String, String> m2 = new HashMap<String, String>();
             
            Iterator<Entry<String, String>> iter1 = m1.entrySet().iterator();
            while(iter1.hasNext()){
                Map.Entry<String, String> entry1 = (Entry<String, String>) iter1.next();
           String m1value = entry1.getValue() == null?"":entry1.getValue();
                String m2value = m2.get(entry1.getKey())==null?"":m2.get(entry1.getKey());
                      
                if (!m1value.equals(m2value)) {//若两个map中相同key对应的value不相等
                    //其他操作...
                }
            }
        }

        /**
         * 用map的entrySet()的增强型for循环(性能效率较高)
         */
        public void compareMap3(){
             Map<String, String> m1 = new HashMap<String, String>();
             Map<String, String> m2 = new HashMap<String, String>();
              
             for(Map.Entry<String, String> entry1:m1.entrySet()){
                 String m1value = entry1.getValue() == null?"":entry1.getValue();
                 String m2value = m2.get(entry1.getKey())==null?"":m2.get(entry1.getKey());
                 if (!m1value.equals(m2value)) {//若两个map中相同key对应的value不相等
                    //其他操作...
                }
             } 
        }
  • 相关阅读:
    算法导论图论图的表示 课后题答案
    MFC 如何添加快捷键
    全排序算法permutation分析与总结
    java k++ 和C/C++ k++的区别
    找回失去的快捷方式向导
    解开注册表中U盘禁止拷贝的限制
    锐捷多网卡解决方案 与当前环境冲突(Code 2)
    Delphi中的Access技巧集
    Delphi MessageBox对话框
    另一个博客,不知道好用不
  • 原文地址:https://www.cnblogs.com/ydymz/p/9388904.html
Copyright © 2020-2023  润新知