• 手机版

    扫码体验手机版

  • 微信公众号

    扫码关注公众号

国内首家协议开发

软芯音视解码保护平台

在线
客服

发布
需求

在线
聊天

天盟
APP

天盟APP下载

关注
微信

微信扫一扫访问
顶部

大神们,请问我这段代码可以优化一下吗,我想减少for循环的使用,能不能整

package com.tianmg.test;import java.util.*;public class clean {   public static void main(String[] args) {      HashMap test1 = new HashMap();      test1.put("GN", "1.1.1");      test1.put("E212", "1");      test1.put("INCTYPE", "新增");      HashMap test2 = new HashMap();      test2.put("GN", "2.2.2");      test2.put("E212", "1");      test2.put("INCTYPE", "删除");      HashMap test3 = new HashMap();      test3.put("GN", "2.2.2");      test3.put("E212", "2");      test3.put("INCTYPE", "新增");      HashMap test4 = new HashMap();      test4.put("GN", "3.3.3");      test4.put("E212", "3");      test4.put("INCTYPE", "删除");      HashMap test5 = new HashMap();      test5.put("GN", "4.4.4");      test5.put("E212", "4");      test5.put("INCTYPE", "删除");      HashMap test6 = new HashMap();      test6.put("GN", "4.4.4");      test6.put("E212", "5");      test6.put("INCTYPE", "新增");      Map stdMap = new HashMap();      List list = new ArrayList();      list.add(test1);      list.add(test2);      list.add(test3);      list.add(test4);      list.add(test5);      list.add(test6);      List list2 = list;      List list3 = new ArrayList();      for (Map map : list) {         for (Map map2 : list2) {            if (map!=map2&&(map.get("E212").equals(map2.get("E212")))) {               if ((map.get("INCTYPE") == "新增" && map2.get("INCTYPE") == "删除") || (map.get("INCTYPE") == "删除" && map2.get("INCTYPE") == "新增")) {                  list3.add(map);                  list3.add(map2);               }            }         }      }      System.out.println("list3的内容,要删除的");      System.out.println(list3);      for (Map map:list3 ){         list.remove(map);      }      System.out.println(list);      for (Map map : list) {         String e212 = map.get("E212").toString();         stdMap.put(e212, map);      }      System.out.println(stdMap);   }}

免责声明:本内容仅代表回答会员见解不代表天盟观点,请谨慎对待。

版权声明:作者保留权利,不代表天盟立场。

使用道具 举报

全部参与1

importjava.util.*;publicclassMain{publicstaticvoidmain(String[]args){HashMaptest1=newHashMap();test1.put("GN","1.1.1");test1.put("E212","1");test1.put("INCTYPE","新增");test1.put("ID","1");HashMaptest2=newHashMap();test2.put("GN","2.2.2");test2.put("E212","2");test2.put("INCTYPE","新增");test2.put("ID","2");HashMaptest3=newHashMap();test3.put("GN","2.2.2");test3.put("E212","1");test3.put("INCTYPE","删除");test3.put("ID","3");HashMaptest4=newHashMap();test4.put("GN","3.3.3");test4.put("E212","3");test4.put("INCTYPE","删除");test4.put("ID","4");HashMaptest5=newHashMap();test5.put("GN","4.4.4");test5.put("E212","4");test5.put("INCTYPE","删除");test5.put("ID","5");HashMaptest6=newHashMap();test6.put("GN","4.4.4");test6.put("E212","5");test6.put("INCTYPE","新增");test6.put("ID","6");HashMaptest7=newHashMap();test7.put("GN","4.4.4");test7.put("E212","5");test7.put("INCTYPE","删除");test7.put("ID","7");HashMaptest8=newHashMap();test8.put("GN","4.4.4");test8.put("E212","4");test8.put("INCTYPE","删除");test8.put("ID","8");MapstdMap=newHashMap();Listlist=newArrayList();list.add(test1);list.add(test2);list.add(test3);list.add(test4);list.add(test5);list.add(test6);list.add(test7);list.add(test8);//Listlist2=list;//Listlist3=newArrayList();System.out.println(list);for(Mapmap:list){if(stdMap.size()==0){Stringrealm=map.get("ID")==null?"":map.get("ID").toString();stdMap.put(realm,map);}Iteratoriter=stdMap.entrySet().iterator();booleanflag=true;while(iter.hasNext()){Map.Entryentry=(Map.Entry)iter.next();Mapval=(Map)entry.getValue();if(map!=val&&map.get("E212").equals(val.get("E212"))){if((map.get("INCTYPE").equals("新增")&&val.get("INCTYPE").equals("删除"))||(map.get("INCTYPE").equals("删除")&&val.get("INCTYPE").equals("新增"))){iter.remove();flag=false;}}}if(flag){Stringrealm=map.get("ID")==null?"":map.get("ID").toString();stdMap.put(realm,map);}}System.out.println(stdMap);}}改了一下,现在成了这样,请问还能优化一下吗

使用道具 举报

发新帖

发布任务需求已有1031167位用户正在使用天盟网服务

发布分类: *
任务预算: *
需求内容: *
手机号码: *
任务商家报价为
  • 预算价 :
  • 成交价 :
  • 完工期 :
  • 质保期 :

* 最终任务项目以服务商报价、双方协商为准!