-1

i have HashMap and its have data, i connect to database by xmlrpc by jetty9 i am call this function by java client , by this code

Object params[] = new Object[]{stString};
HashMap v1;
v1 = (HashMap<String, Object[]>)server.execute("DBRamService.getRmsValues", params);

i need to print it in my java client , how can i make it ? this is my function that get data from 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;  }
King imbrator
  • 27
  • 1
  • 9

2 Answers2

0

Following is the snippet to loop through the vArrL and printing the values:

    for (int i=0;i<vaArrL.size();i++) {
       System.out.println(vaArrL.get(i));
    }

Looping through HashMap using Iterator:

    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);
        }           
    }
bprasanna
  • 2,423
  • 3
  • 27
  • 39
0

Here is how to iterate through a HashMap and get all the keys and values:

// 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()));
}

If you need to print in a different format, you can iterate over the elements of the value array like this:

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);
    }
}
Ian Lovejoy
  • 376
  • 4
  • 7