Raj*_*esh 2 java android arraylist
假设我有这样的 Json 响应:
{
"status": true,
"data": {
"29": "Hardik sheth",
"30": "Kavit Gosvami"
}
}
Run Code Online (Sandbox Code Playgroud)
我正在使用 Retrofit 解析 Json 响应。根据这个答案,我将不得不使用Map<String, String>它来提供地图中的所有数据。现在我想要的是ArrayList<PojoObject>.
PojoObject.class
public class PojoObject {
private String mapKey, mapValue;
public String getMapKey() {
return mapKey;
}
public void setMapKey(String mapKey) {
this.mapKey = mapKey;
}
public String getMapValue() {
return mapValue;
}
public void setMapValue(String mapValue) {
this.mapValue = mapValue;
}
}
Run Code Online (Sandbox Code Playgroud)
将 a 转换Map<key,value>为 a的最佳方法是List<PojoObject>什么?
如果您可以扩展您的类以让构造函数也接受这些值:
map.entrySet()
.stream()
.map(e -> new PojoObject(e.getKey(), e.getValue()))
.collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
如果你不能:
map.entrySet()
.stream()
.map(e -> {
PojoObject po = new PojoObject();
po.setMapKey(e.getKey());
po.setMapValue(e.getValue());
return po;
}).collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
请注意,这使用 Java 8 StreamAPI。
| 归档时间: |
|
| 查看次数: |
9745 次 |
| 最近记录: |