将对象存储到HashMap中

时间:2016-05-18 16:31:36

标签: java xml jaxb hashmap key-value

我有一个XML,我正在使用JAXB以下列方式转换为java对象:

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

我希望将其存储到HashMap<"Deposits_1M", 2016-06-29 -0.00029)格式的哈希映射中,其中1M是我们通过curve.getTenor()得到的值,而2016-06-29是其中一个值我们可以做curve.getMaturitydate()&amp;我-.00029 curve.getParrate()for(Curvepoint curve:list2)所以,基本上我希望我们从getTenor() "Deposits"的每次迭代得到的值与字符串curve.getMaturitydate(一起作为键,以及我们从curve.getParrate()得到的值和IsMdiContainer = true作为一个哈希映射条目的值。

我该怎么做?

2 个答案:

答案 0 :(得分:1)

Java集合映射(包括HashMap)是参数化的,因此您需要在声明中包含键和值类型:

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

然后创建一个条目,你使用Map.put,所以在你的情况下我认为这将是:

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

答案 1 :(得分:0)

寻找一个具有多个值的1个键映射的结构?

{{1}}

然后在你的struct中,所有值都将存储在ArrayList Position 0-1中。 如果您不想拆分字符串或将它们存储为concat。此外,如果值更改并存储多个答案。每2个数组索引就是一对值。