簡體   English   中英

如何動態地從 hashMap 獲取值 - Java

[英]How can I get values from a hashMap dynamically - Java

我正在與 HashMap 合作,還沒有太多經驗。

我正在嘗試編寫一個 csvFile 來證明兩個列表之間的比較。 如果比較值相同ok否則不 ok

很簡單,但是值“ok”或“not ok”需要自動更改,所以有人建議我使用 HashMap,我把比較字段的名稱作為鍵,值將是它的 state(好的或者不行)。

到目前為止,返回值並寫入文件,但狀態不會自動填寫。

到目前為止,這是我的代碼,如果有人知道我該怎么做或有其他建議,請告訴我。

HashMap

public static Map<String, String> initMap(String status) {
         Map<String, String> mapFields = new HashMap<String, String>();
         
         mapFields.put("Book Ref", status);
         mapFields.put("Trade Date", status);
         mapFields.put("Start Date", status);
         mapFields.put("End Date", status);
         mapFields.put("Period Multiplier", status);
         mapFields.put("Notional", status);
         mapFields.put("Currency", status);
         mapFields.put("Rate", status);
         mapFields.put("Frequency Multiplier", status);
         mapFields.put("Day count", status);
         
        return mapFields;
 
    }

在同一個 class 中,我創建了這個方法來比較兩個列表並定義它是否正常。

public static void compareValues(List<String> inputs, List<String> outputs, XrayFields fields, TradeData data, MKTWireIRS mkt) throws ParseException, InterruptedException {
        int y = 0;
        int x = 0;
        
        Map<String, String> map = new HashMap<String, String>();
    //   List<WriterCSV> writeCSV = new ArrayList<>();
         WriterCSV cv = new WriterCSV();
         
        try {
            for (String input : inputs) {   
                for (String out : outputs) {
                     cv = new WriterCSV();
                     map = new HashMap<String, String>();
                    if (y == x) {
                        if (input.equals(out)) {
                            System.out.println("ok: " + input + " = " + out);                       
                            String comment = "All fields checked are ok";
                            fields.setComment(comment);
                            fields.setStatus("PASS");
                            
                            cv.setOk("Ok");
                            map = initMap(cv.getOk());
                            
                        } else {
                            System.out.println("not ok: " + input + " = " + out);
                            fields.setStatus("FAIL");
                            String comment = "The value " + input + " is not the same as " + out;
                            fields.setComment(comment);
                            
                            cv.setOk("not Ok");
                            map = initMap(cv.getOk());

                        }
                    }
                    x = x + 1; // count of the list of output

                }
                
                y = y + 1; // count of the list of inputs
                x = 0; // reset to 0 the count of outputs
            }
            
            //create evidence of comparison
             cv.reportMKTWireToOutputIRS(data, mkt, map);
                        
        } catch (Error e) {
            System.out.println(e);
        }
    }

這是寫csv的方法。

public void reportMKTWireToOutputIRS(TradeData data2, MKTWireIRS mkt, Map<String, String> map ) throws ParseException, InterruptedException {
        try {
            FileWriter fw = new FileWriter(new File(CSV_MKTWire + Setup.IRScsv));
            CSVWriter cw = new CSVWriter(fw);
            
            //format values
            String month = PropertyNames.deCapitalize(data2.getPeriod());
            String monthReset = PropertyNames.deCapitalize(data2.getResetFrequencyPeriod());
            String formatTradeDateMKT = Utils.formatDateToCompareMKTWire(data2.getTradeDateIRS());
            String formatStartDateMKT = Utils.formatDateToCompareMKTWire(data2.getStart_Date());
            String formatMAturityDateMKT = Utils.formatDateToCompareMKTWire(data2.getMaturity_Date());
            String rateActual = Utils.roundDecimal(data2.getRateIRS());
            String rateFormat = Utils.roundRateMKTwire(mkt.getRateIRS());
            String notionalFormat = data2.getNotional().charAt(0) + "M";

            String[] headers = { "Output Field", "Output Value", " MKTWire Field", " MKTWire Value", "Status" };
            List<String[]> data = new ArrayList<String[]>();

                String[] book = { "Book Ref", data2.getBookRef() + data2.getBookType(),"Book MKTWire",mkt.getBookIRS(), map.get("Book Ref")};
                String[] tradeDate = { "Trade Date", formatTradeDateMKT,"Trade Date MKTWire",mkt.getTradeDateIRS(), map.get("Trade Date")};
                String[] startDate = { "Start Date", formatStartDateMKT, "Start Date MKTWire",mkt.getStartDate(), map.get("Start Date") };
                String[] maturity = { "End Date", formatMAturityDateMKT, "End Date MKTWire",mkt.getEndDate(), map.get("End Date") };
                String[] tenor = { "Period Multiplier", data2.getPeriodMultiplier() + month, "Tenor MKTWire",mkt.getTenorIRS(), map.get("Period Multiplier") };
                String[] notional = { "Notional", notionalFormat, "Notional MKTWire", mkt.getNotionalValueIRS(), map.get("Notional") };
                String[] currency = { "Currency", data2.getCurrencyIRS(), "Currency MKTWire", mkt.getCurrencyIRS(), map.get("Currency") };
                String[] rate = { "Rate", rateActual, "Rate MKTWire", rateFormat, map.get("Rate") };
                String[] resetFrequency = { "Frequency Multiplier", data2.getResetFrequencyMultiplier() + monthReset, "Frequency Multiplier MKTWire", mkt.getResetFrequencyIRS(),map.get("Frequency Multiplier") };
                String[] dayCount = { "Day Count", data2.getDayCount(), "Day Count MKTWire", mkt.getDayCountIRS(), map.get("Day count") };

            data.add(headers);
            data.add(book);
            data.add(tradeDate);
            data.add(startDate);
            data.add(maturity);
            data.add(tenor);
            data.add(notional);
            data.add(currency);
            data.add(rate);
            data.add(resetFrequency);
            data.add(dayCount);
            
            
            cw.writeAll(data);
            cw.flush();
            fw.flush();
            cw.close();
            fw.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

您有一個 map 並且您正在調用 initMap 方法,該方法在一個循環中為 map 中的所有鍵設置值,最后根據您的最終循環驗證,它將有“ok”或“not ok”。

暫無
暫無

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

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