Java打印Map<String, Object>中的值对象

huangapple go评论129阅读模式
英文:

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&lt;DataModel&gt; dataList = new ArrayList&lt;DataModel&gt;();

parsed on class

for(DataModel list : dataList) {
	final String[] data = {list.getVar_a(), list.getVar_b()};
	System.out.println(&quot;out data&quot;);
	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 &amp; 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&lt;String, Object&gt; mapData = new LinkedHashMap&lt;String, Object&gt;();

when I set data on map, its result from database

Map&lt;String, Object&gt; map = new LinkedHashMap&lt;String, Object&gt;();
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&lt;String, Object&gt; data = new LinkedHashMap&lt;&gt;();
		for (int i = 1; i &lt;= count; i++ ) {
			String name = rsmd.getColumnName(i);
			data.put(name, rs.getObject(name));
			
		}
		map.put(&quot;&quot;+jml, data);
		jml++;
	} while(rs.next());
	
	setStatus(SUCCESS);
	setMessage(msg);
} else {
	LOG.info(NO_DATA_FOUND);
}

parsed on class

for(Map.Entry&lt;String,Object&gt; list : mapData.entrySet()) {
	String key = list.getKey();
	Object val = list.getValue();
	System.out.println(&quot;out data&quot;);
	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&lt;String,Object&gt; list : mapData.entrySet()) {
        DataModel value = (DataModel) list.getValue();
        System.out.println(&quot;out data&quot;);
        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&lt;String,Object&gt; list : mapData.entrySet()) {
	String key = list.getKey();
	Object val = list.getValue();
	System.out.println(&quot;out data&quot;);
	System.out.println(key);
	System.out.println(val);
}

What you really need is a map of map as I am showing below.

Map&lt;String, Map&lt;String, Object&gt;&gt; map = new LinkedHashMap&lt;String, Map&lt;String, Object&gt;&gt;()

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&lt;String,Map&lt;String, Object&gt; list : mapData.entrySet()) {
    String key = list.getKey();
    System.out.println(&quot;out data&quot;);
    for(Map.Entry&lt;String,Object&gt; innermap : list.getValue().entrySet()) {
        //System.out.println(innermap.getKey());
        System.out.println(innermap.getValue());
    }
}

huangapple
  • 本文由 发表于 2020年8月28日 22:08:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/63635438.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定