我有HashMap并且有数据, 我通过jetty9通过xmlrpc连接到数据库 我通过此代码
通过java客户端调用此函数Object params[] = new Object[]{stString};
HashMap v1;
v1 = (HashMap<String, Object[]>)server.execute("DBRamService.getRmsValues", params);
我需要在我的java客户端打印它,我该怎么做? 这是我从datebase获取数据的函数
HashMap<String, Object[]> result = new HashMap<String, Object[]>();
ArrayList<Double> vaArrL = new ArrayList<Double>();
try {
// i have connected to postgres DB and get data
while (rs.next()){
vaArrL.add(rs.getDouble("va"));
}
int sz = vaArrL.size();
result.put("va", vaArrL.toArray(new Object[sz]));
} catch ( Exception e ) {
System.out.println(e);
e.printStackTrace();
}
return result; }
答案 0 :(得分:0)
以下是循环播放vArrL
并打印值的代码段:
for (int i=0;i<vaArrL.size();i++) {
System.out.println(vaArrL.get(i));
}
使用Iterator循环使用HashMap:
Iterator<Entry<String, Object[]>> it = result.entrySet().iterator();
while (it.hasNext()) {
Entry<String, Object[]> pairs = (Entry<String, Object[]>) it.next();
for(Object obj: pairs.getValue()) {
System.out.println(obj);
}
}
答案 1 :(得分:0)
以下是如何遍历HashMap并获取所有键和值:
// example hash map
HashMap<String, Object[]> v1 = new HashMap<String, Object[]>();
v1.put("hello", new Object[] {"a", "b"});
// print keys and values
for(Map.Entry<String, Object[]> entry : v1.entrySet()) {
System.out.println("Key: " + entry.getKey() + " Values: " + Arrays.asList(entry.getValue()));
}
如果需要以不同的格式打印,可以像这样迭代值数组的元素:
for(Map.Entry<String, Object[]> entry : v1.entrySet()) {
System.out.println("Key:");
System.out.println(entry.getKey());
System.out.println("Values:");
for (Object valueElement : entry.getValue()) {
System.out.println(valueElement);
}
}