简体   繁体   中英

How can I get values from a hashMap dynamically - Java

I am working with HashMap and don't have much experience yet.

I am trying to write a csvFile for proof of a comparison between two lists. If the compared value is the same ok otherwise not 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).

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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