将ArrayList中某个字段的所有出现与另一个ArrayList中的同一字段进行匹配(通过搜索和匹配进行循环)

时间:2018-06-26 10:40:33

标签: java arraylist

我喜欢将cweids中包含的ArrayList Acweids中包含的ArrayList B的所有匹配项匹配,以便处理cweid的每个匹配项。由于必须记录并处理每种情况,因此我无法使用Sets,因此实际上都是从数据库中查询两个ArrayList。请在下面查看我所做的代码片段。循环仅遍历ArrayList B,如何使迭代完成?

public static void vulnerabilityCorrelator(String imageName, String microserviceName) {

    ScannerDAO.em.getTransaction().begin();

    List<Anchore> imageVulns = new ArrayList<Anchore>();
    Map<String, String> correlatedVulns = new HashMap<String, String>();

    Query appQ = ScannerDAO.em
            .createQuery("SELECT a FROM Alert a WHERE a.microserviceName='" + microserviceName + "'");
    Query imageQ = ScannerDAO.em.createQuery("SELECT i FROM Anchore i  WHERE i.imageName='" + imageName + "'");

    int count = 0;
    imageVulns = imageQ.getResultList();
    List<Alert> appVulns   = appQ.getResultList();
    System.out.println("appVulns.size() " + appVulns.size());

    for (int i = 0; i < appVulns.size(); i++) {

        Alert alert = appVulns.get(i);
        System.out.println(alert);

        String appCweId = alert.getCweid();
        for (Anchore imageV : imageVulns) {

            List<Vulnerability> vulnList = imageV.getVulnerabilities();

            for (Vulnerability vulnerability : vulnList) {

                String imageCweId = vulnerability.getCweid();
                System.out.println(imageCweId);

                String imageCweIdR = null;
                if (imageCweId == null || imageCweId.equals("None")) {

                    System.out.println(vulnerability.getVuln() + " does not have a CWE ID ");
                } else {

                    String splitimageCweId[] = vulnerability.getCweid().split("-");
                    imageCweIdR = splitimageCweId[1];

                }
                if (appCweId.equalsIgnoreCase(imageCweIdR) || appCweId == imageCweIdR) {
                    System.out.println("correlated  appCweId : " + appCweId + " imageCweId :  " + imageCweIdR);
                    count++;
                    System.out.println("counting " + count);
                    correlatedVulns.put(vulnerability.getVuln(), vulnerability.getCweid());
                    System.out.println(vulnerability.getVuln() + " : " + vulnerability.getCvssScore());
                } else {
                    System.out.println("not correlated appCweId : " + appCweId + " imageCweId : " + imageCweIdR);
                }

            }

        }
        System.out.println(" correlated count : " + count);

        System.out.println(alert.getAlert());

    }


}

1 个答案:

答案 0 :(得分:0)

以下代码解决了该问题:

public static Map<String, String> vulnerabilityCorrelator(String imageName, String microserviceName) {

    ScannerDAO.em.getTransaction().begin();

    List<Anchore> imageVulns = new ArrayList<Anchore>();
    List<Alert> appVulns = new ArrayList<>();
    Map<String, String> correlatedVulns = new HashMap<String, String>();
    Map<String, String> correlatedAppInfo = new HashMap<String, String>();

    Query appQ = ScannerDAO.em
            .createQuery("SELECT a FROM Alert a WHERE a.microserviceName='" + microserviceName + "'");
    Query imageQ = ScannerDAO.em.createQuery("SELECT i FROM Anchore i  WHERE i.imageName='" + imageName + "'");

    int count = 0;
    int noncorrelatedcount = 0;
    int loopingcounts = 0;

    imageVulns = imageQ.getResultList();
    appVulns = appQ.getResultList();

    List<Vulnerability> vulnList = new ArrayList();
    for (Anchore imageV : imageVulns) {

        vulnList = imageV.getVulnerabilities();
    }


    int loop = 0;
    for (Alert al : appVulns) {

        Alert alert = appVulns.get(loop);
        for (int j = 0; j < vulnList.size(); j++) {

            Vulnerability vulner = vulnList.get(j);
            String appCweId = alert.getCweid();
            String imageCweId = vulner.getCweid();
            String imageCweIdR = null;
            if (imageCweId == null || imageCweId.equals("None")) {

                System.out.println(vulner.getVuln() + " does not have a CWE ID ");
            } else {

                String splitimageCweId[] = imageCweId.split("-");
                imageCweIdR = splitimageCweId[1];


            }
            if (appCweId.equalsIgnoreCase(imageCweIdR) || appCweId == imageCweIdR) {
                System.out.println("correlated  appCweId : " + appCweId + " imageCweId :  " + imageCweIdR);
                count++;
                System.out.println("counting " + count);
                correlatedVulns.put(vulner.getVuln(), vulner.getPackage());
                correlatedAppInfo.put(alert.getAlert(), alert.getCweid());
                System.out.println(vulner.getVuln() + " : " + vulner.getCvssScore());
            } else {
                System.out.println("not correlated appCweId : " + appCweId + " imageCweId : " + imageCweIdR);
                noncorrelatedcount++;
            }

        }
        loop++;
    }

     for (int k = 0; k < correlatedVulns.size(); k++) {

     System.out.println("correlatedVulns " + k +  " : " +  correlatedVulns);
     }

     for (int l = 0; l < correlatedAppInfo.size(); l++) {

         System.out.println("correlatedAppInfo " + l + "  : "  +  correlatedAppInfo);
         }

    return correlatedVulns;

}