我有一个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()
和-.00029
获得的值之一我们可以通过curve.getParrate()
获得。因此,基本上,我希望我们从for(Curvepoint curve:list2)
的每次迭代获得的值与字符串getTenor()
一起作为键,并且从"Deposits"
和curve.getMaturitydate(
的值作为键。一个哈希映射条目的值。我怎么做?
最佳答案
Java集合地图(包括HashMap)是参数化的,因此您需要在声明中包括键和值类型:
Map<String,String> resultsMap = new HashMap<>();
然后使用Map.put创建一个条目,因此在您的情况下,我认为这是:
resultsMap.put(curve.getTenor(), curve.getMaturitydate()+" "+curve.getParrate())