我有这段代码来检索地图的键值对:
countries = BeneficiaryJSONParser.parseCountriesFeed(result);
for(Map.Entry<String, Object> entry: countries.entrySet()) {
System.out.println(entry.getKey() + " : " + entry.getValue());
}
输出:
data : [{234=United Arab Emirates, 103=India, 210=Sri Lanka, 235=United Kingdom, 216=Switzerland, 200=Singapore, 76=France}]
status : 200
code : 1
我希望将数据作为具有键和值的数组。因为,我想把这些键值对放在
中final MyData items[] = new MyData[3];
items[0] = new MyData( "234","United Arab Emirates" );
items[1] = new MyData( "103","India" );
items[2] = new MyData( "210","Sri Lanka" );
.
.
.
答案 0 :(得分:4)
您只需要将每个条目转换为MyData:
List<MyData> data = new ArrayList<> ();
for (Map.Entry<String, Object> e: countries.entrySet()) {
data.add(new MyData(e.getKey(), String.valueOf(e.getValue());
}
final MyData[] items = data.toArray(new MyData[0]);
或使用流:
final MyData[] items = countries.entrySet().stream()
.map(e -> new MyData(e.getKey(), String.valueOf(e.getValue()))
.toArray(MyData[]::new);