簡體   English   中英

每當有關鍵匹配時,減去兩個地圖的值?

[英]Subtracting values of two maps whenever there is a key match?

我將解釋邏輯:我正在讀取一個包含許多soap格式的請求和響應的XML文件,然后我將請求和響應存儲在兩個哈希映射中。 在第一個哈希映射中,我將事務 ID(唯一)存儲為鍵,並將值存儲為請求時間,直到名稱。 在第二個哈希映射中,我將事務 ID(唯一)存儲為鍵,將值存儲為響應時間。 在兩個哈希映射中,鍵相同但值不同,通過使用 for 循環迭代兩個循環,我需要獲得響應時間和請求時間之間的時間差,例如:請求時間:2020-01-30T11:07:08.351Z 和響應時間:2020-01-30T11:07:10.152Z

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();
                 }   

    }

}

我不確定我是否正確理解了您的問題,但也許您可以執行以下類似的操作:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM