只要有一个键匹配,就减去两个Map的值?

nvbavucw  于 2021-07-12  发布在  Java
关注(0)|答案(1)|浏览(285)

我将解释逻辑:我正在读取一个xml文件,其中包含许多soap格式的请求和响应,然后我将请求和响应存储在两个哈希Map中。在第一个哈希Map中,我将事务id(unique)存储为键,将值存储为请求时间til name。在第二个哈希Map中,我将事务id(unique)存储为键,将值存储为响应时间。在两个hashMap中,键是相同的,但是值是不同的,通过使用for循环迭代两个循环,我需要得到响应时间和请求时间之间的时间差,例如:requesttime:2020-01-30t11:07:08.351z和响应time:2020-01-30t11:07:10.152赫兹

public class MapTimeDiff {
public static void main(String[] args) throws ParseException {

File file =new File("C:\\Users\\gsanaulla\\Documents\\My Received Files\\ecarewsframework.xml");
        Scanner in = null;
        String tilname = null;
        String transactionId = null;
        String requesttime = null;
        String responsetime = null;
        Date dateOne = null;
        Date dateTwo = null;
        double timeDiff;
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
        Map<String,ArrayList<String>> request=new HashMap<String,ArrayList<String>>(); 
          ArrayList<String> req=new ArrayList<String>();
        Map<String,ArrayList<String>> response=new HashMap<String,ArrayList<String>>(); 
        ArrayList<String> res=new ArrayList<String>();
        try {
            in = new Scanner(file);
            while(in.hasNext())
                {
                String line=in.nextLine();
                if(line.contains("</S:Envelope>")) {
                    System.out.println(line);

                    tilname=line.split("StartRecord><")[1].split("><")[0].split(":")[1];
                    System.out.println("tilname :: "+tilname);
                    transactionId = line.split("transactionId>")[1].split("<")[0];
                    System.out.println("transactio id :: "+transactionId);
                    requesttime=line.split("sourceTimestamp>")[1].split("<")[0];
                    System.out.println("request time is :: "+requesttime);
                    dateOne = df.parse(requesttime);

                }
                req.add(tilname);
                req.add(dateOne.toString());
                System.out.println("req is==== " +req);
                request.put(transactionId,req);
                System.out.println("request is==== " +request.get(transactionId));
                    if(line.contains("</SOAP-ENV:Envelope>")) {
                        //System.out.println(line);

                          if(line.contains("transactionId")) 
                          { 
                              responsetime=line.split("sourceTimestamp>")[1].split("<")[0];
                              transactionId = line.split("transactionId>")[1].split("<")[0];
                              System.out.println("responsetime :: "+responsetime); 
                              System.out.println("transaction id "+transactionId);
                              dateTwo = df.parse(responsetime);

                          }
                          res.add(dateTwo.toString());
                          System.out.println("res is===== "+res);
                          response.put(transactionId,res);
                          System.out.println("response is===== "+response.get(transactionId));
                        for (Entry<String, ArrayList<String>> entry : request.entrySet()) {
                              for (Entry<String, ArrayList<String>> entry1 : response.entrySet()) {
                             System.out.println("Key = " + entry.getKey() + 
                                         ", Value = " + entry.getValue());
                              System.out.println("Key = " + entry1.getKey() + 
                                         ", Value = " + entry1.getValue());
                                  if(request.keySet().equals(response.keySet())) {
                                      timeDiff = (dateTwo.getTime() - dateOne.getTime());
                                  }
                          }
                          } 
}
      }             
        }

           catch (FileNotFoundException e) {
                 // TODO Auto-generated catch block
                 e.printStackTrace();
                 }   

    }

}
06odsfpq

06odsfpq1#

我不确定我是否正确理解了你的问题,但也许你可以做如下类似的事情:

Map<String, List<String>> requests = Map.of("1", List.of("10,13,12"), "2", List.of("8,7,9"), "3", List.of("11"));
Map<String, List<String>> responses = Map.of("1", List.of("9,10,14"), "2", List.of("8,9,6,12"));

for(Map.Entry<String, List<String>> requestEntry : requests.entrySet()) {
    String transactionId = requestEntry.getKey();
    if(responses.containsKey(transactionId)) {
        System.out.println("Transaction Id: " + transactionId);
        for(int i = 0; i < min(requestEntry.getValue().size(), responses.get(transactionId).size()); i++) {
            List<String> requestTimes = asList(requestEntry.getValue().get(i).split(","));
            List<String> responseTimes = asList(responses.get(transactionId).get(i).split(","));
            for(int j = 0; j < min(requestTimes.size(), responseTimes.size()); j++) {
                int requestTime = parseInt(requestTimes.get(j));
                int responseTime = parseInt(responseTimes.get(j));
                System.out.println("Difference: " + abs(requestTime - responseTime));
            }
        }
    }
}

如您所见,transactionid 3没有响应,因此将忽略此项。如果键的列表中的元素大小不同(transactionid 2),多余的元素也将被忽略。

Transaction Id: 1
Difference: 1
Difference: 3
Difference: 2
Transaction Id: 2
Difference: 0
Difference: 2
Difference: 3

相关问题