user3591433
user3591433

Reputation: 115

Storing objects into a HashMap

I have an XML which I'm converting to a java object using JAXB in the following way:

package IRCurves;

import java.io.File;
import java.util.HashMap;
import java.util.List;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

public class XmlToObject {
    public static void main(String[] args) {
        try {
            File file = new File("InterestRates_JPY_20160426.xml");
            JAXBContext jaxbContext = JAXBContext.newInstance(InterestRateCurve.class);

            Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
            InterestRateCurve ir= (InterestRateCurve) jaxbUnmarshaller.unmarshal(file);

            System.out.println(ir.getEffectiveasof()+" "+ir.getCurrency()+" "+ir.getBaddayconvention());
            System.out.println("Deposits:");
            List<Deposits> list=ir.getDeposits();
            for(Deposits ans:list) {
                System.out.println(ans.getDaycountconvention()+" "+ans.getSnaptime()+" "+ans.getSpotdate());
                System.out.println("Calenders:");
                List<Calenders> list1=ans.getCalenders();
                for(Calenders c:list1)
                    System.out.println(c.getCalender());
                System.out.println("Curvepoint:");
                List<Curvepoint> list2=ans.getCurvepoint();
                for(Curvepoint curve:list2)
                    System.out.println(curve.getTenor()+" "+curve.getMaturitydate()+" "+curve.getParrate());
            }
            System.out.println("Swaps:");
            List<Swaps> list3=ir.getSwaps();
            for(Swaps swap:list3) {
                System.out.println(swap.getFixeddaycountconvention()+" "+swap.getFloatingdaycountconvention()+" "+swap.getFixedpaymentfrequency()+" "+swap.getFloatingpaymentfrequency()+" "+swap.getSnaptime()+" "+swap.getSpotdate());
            /*System.out.println("Calenders:");  
            List<Calenders> list1=swap.getCalenders();
            for(Calenders c:list1)  
                System.out.println(c.getCalender());*/
                System.out.println("Curvepoint:");
                List<Curvepoint> list2=swap.getCurvepoint();
                for(Curvepoint curve:list2)
                    System.out.println(curve.getTenor()+" "+curve.getMaturitydate()+" "+curve.getParrate());
            }
        } catch (JAXBException e) {
            e.printStackTrace();
        }
    }
}

I want to store this into a Hash Map of the form HashMap<"Deposits_1M", 2016-06-29 -0.00029), where 1M is the value we get by doing curve.getTenor() and 2016-06-29 is one of the value we get by doing curve.getMaturitydate() & -.00029 we get by doing curve.getParrate(). so, basically I want the value we get from each iteration of for(Curvepoint curve:list2) for getTenor() to be as key along with the string "Deposits", and the value we get from curve.getMaturitydate() and curve.getParrate() to be as the value of one hash map entry.

How do I do that?

Upvotes: 0

Views: 1172

Answers (2)

Joey
Joey

Reputation: 203

Looking for a structure to have 1 key map to multiple values?

HashMap<String,List<Integer> results = new HashMap<String,List<Integer>();
// Does Key already exist?
List<Integer> temp = results.get(curve.getTenor());

       if(temp != null){

               //if it does add to object
               temp.add(curve.getMaturitydate());
               temp.add(curve.getParrate());


       }else{
           // if not make object and put in has map
       List<Integer> nn = new ArrayList<Integer>();

       nn.add(curve.getMaturitydate());
       nn.add(curve.getParrate());
       userList.put(curve.getTenor(), nn);

           }

Then in your struct all values would be stored in ArrayList Position 0-1. If you didn't want to split strings or store them as a concat. Also now if values change and store multiple answers. every 2 array indexes are a pair of values.

Upvotes: 0

John Coker
John Coker

Reputation: 173

Java collections Maps (including HashMap) are parametric, so you need to include the key and value types in the declaration:

Map<String,String> resultsMap = new HashMap<>();

Then to create an entry, you use Map.put, so in your case I think this would be:

resultsMap.put(curve.getTenor(), curve.getMaturitydate()+" "+curve.getParrate())

Upvotes: 1

Related Questions