英文:
Java Print Value Object on Map<String, Object>
问题
代码部分不要翻译,只返回翻译好的内容:
I set list on another class
在另一个类中设置了列表
List<DataModel> dataList = new ArrayList<DataModel>();
在类中解析
for(DataModel list : dataList) {
final String[] data = {list.getVar_a(), list.getVar_b()};
System.out.println("out data");
System.out.println(list.getVar_a());
System.out.println(list.getVar_b());
}
这将打印数据
out data
val_a
val_b
模型类
class DataModel {
private String var_a, var_b;
//Getter & Setter
}
但现在,我在另一个类中使用和设置了映射,并且我没有实现一个模型类,因为在实际情况下它有太多的变量。
Map<String, Object> mapData = new LinkedHashMap<String, Object>();
当我在映射中设置数据时,它来自数据库的结果
Map<String, Object> map = new LinkedHashMap<String, Object>();
msg = (String) cs.getObject(5);
rs = (ResultSet) cs.getObject(4);
ResultSetMetaData rsmd = rs.getMetaData();
int count = rsmd.getColumnCount();
if(rs.next()){
int jml = 0;
do {
Map<String, Object> data = new LinkedHashMap<>();
for (int i = 1; i <= count; i++ ) {
String name = rsmd.getColumnName(i);
data.put(name, rs.getObject(name));
}
map.put(""+jml, data);
jml++;
} while(rs.next());
setStatus(SUCCESS);
setMessage(msg);
} else {
LOG.info(NO_DATA_FOUND);
}
在类中解析
for(Map.Entry<String,Object> list : mapData.entrySet()) {
String key = list.getKey();
Object val = list.getValue();
System.out.println("out data");
System.out.println(key);
System.out.println(val);
}
这将打印数据
out data
0
{var_a=val_a, var_b=val_b}
我想要像这样从对象中获取值
out data
val_a
val_b
英文:
I set list on another class
List<DataModel> dataList = new ArrayList<DataModel>();
parsed on class
for(DataModel list : dataList) {
final String[] data = {list.getVar_a(), list.getVar_b()};
System.out.println("out data");
System.out.println(list.getVar_a());
System.out.println(list.getVar_b());
}
this prints data
out data
val_a
val_b
Model Class
class DataModel {
private String var_a, var_b;
//Getter & Setter
}
But now, I use and set map on another class and I'm not implementing a model class because in real case it has too many variables.
Map<String, Object> mapData = new LinkedHashMap<String, Object>();
when I set data on map, its result from database
Map<String, Object> map = new LinkedHashMap<String, Object>();
msg = (String) cs.getObject(5);
rs = (ResultSet) cs.getObject(4);
ResultSetMetaData rsmd = rs.getMetaData();
int count = rsmd.getColumnCount();
if(rs.next()){
int jml = 0;
do {
Map<String, Object> data = new LinkedHashMap<>();
for (int i = 1; i <= count; i++ ) {
String name = rsmd.getColumnName(i);
data.put(name, rs.getObject(name));
}
map.put(""+jml, data);
jml++;
} while(rs.next());
setStatus(SUCCESS);
setMessage(msg);
} else {
LOG.info(NO_DATA_FOUND);
}
parsed on class
for(Map.Entry<String,Object> list : mapData.entrySet()) {
String key = list.getKey();
Object val = list.getValue();
System.out.println("out data");
System.out.println(key);
System.out.println(val);
}
this prints data
out data
0
{var_a=val_a, var_b=val_b}
I want to get value on object like this
out data
val_a
val_b
答案1
得分: -1
将您的地图循环更改为:
for (Map.Entry<String, Object> list : mapData.entrySet()) {
DataModel value = (DataModel) list.getValue();
System.out.println("输出数据");
System.out.println(value.getVar_a());
System.out.println(value.getVar_b());
}
英文:
Change your Map for loop to:
for(Map.Entry<String,Object> list : mapData.entrySet()) {
DataModel value = (DataModel) list.getValue();
System.out.println("out data");
System.out.println(value.getVar_a());
System.out.println(value.getVar_b());
}
答案2
得分: -2
以下是翻译好的内容:
首先,您有一个对象的映射,而您实际需要的是一个嵌套映射(Map of Map)。
以下是您正在读取对象映射的代码:
for (Map.Entry<String, Object> list : mapData.entrySet()) {
String key = list.getKey();
Object val = list.getValue();
System.out.println("输出数据");
System.out.println(key);
System.out.println(val);
}
您实际需要的是一个嵌套映射,如下所示:
Map<String, Map<String, Object>> map = new LinkedHashMap<String, Map<String, Object>>();
现在,如果您将其视为嵌套映射,您可以获得所需的结果。
0
: {a1:v1, a2:v2, 等...},
1
: {a2:v2, a2:v2, 等...},
2
: {a3:v3, a3:v3, 等...}
以下是读取嵌套映射的代码:
for (Map.Entry<String, Map<String, Object>> list : mapData.entrySet()) {
String key = list.getKey();
System.out.println("输出数据");
for (Map.Entry<String, Object> innermap : list.getValue().entrySet()) {
//System.out.println(innermap.getKey());
System.out.println(innermap.getValue());
}
}
英文:
First you have a map of object, and what you really need is a Map of Map.
Here is your reading a map of object.
for(Map.Entry<String,Object> list : mapData.entrySet()) {
String key = list.getKey();
Object val = list.getValue();
System.out.println("out data");
System.out.println(key);
System.out.println(val);
}
What you really need is a map of map as I am showing below.
Map<String, Map<String, Object>> map = new LinkedHashMap<String, Map<String, Object>>()
Now if you read it as map of map, You can have the result that you need.
0
: {a1:v1, a2:v2, etc...},
1
: {a2:v2, a2:v2, etc...},
2
: {a3:v3, a3:v3, etc...}
for(Map.Entry<String,Map<String, Object> list : mapData.entrySet()) {
String key = list.getKey();
System.out.println("out data");
for(Map.Entry<String,Object> innermap : list.getValue().entrySet()) {
//System.out.println(innermap.getKey());
System.out.println(innermap.getValue());
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论