简体   繁体   English

如何动态地从 hashMap 获取值 - Java

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

I am working with HashMap and don't have much experience yet.我正在与 HashMap 合作,还没有太多经验。

I am trying to write a csvFile for proof of a comparison between two lists.我正在尝试编写一个 csvFile 来证明两个列表之间的比较。 If the compared value is the same ok otherwise not ok .如果比较值相同ok否则不 ok

Simple, but the values "ok" or "not ok" need to be changed automatically, so it was suggested to me to use HashMap where I put the name of the compared field which is the key and the value will be its state (ok or not ok).很简单,但是值“ok”或“not ok”需要自动更改,所以有人建议我使用 HashMap,我把比较字段的名称作为键,值将是它的 state(好的或者不行)。

So far the values are returned and the file is written, but the status does not fill in automatically.到目前为止,返回值并写入文件,但状态不会自动填写。

This is my code so far, if anyone knows how I can do it or has other suggestions please let me know.到目前为止,这是我的代码,如果有人知道我该怎么做或有其他建议,请告诉我。

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

Here in the same class, I created this method to compare two lists and define if it is ok or not.在同一个 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);
        }
    }

This is the method for writing the csv.这是写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();
        }
    }

You are having one map and you are calling the initMap method which sets the value for all keys in the map within a loop, in the end it will have either "ok" or "not ok" based on your final loop validation.您有一个 map 并且您正在调用 initMap 方法,该方法在一个循环中为 map 中的所有键设置值,最后根据您的最终循环验证,它将有“ok”或“not ok”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM