0

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
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Budi Santoso
  • 13
  • 1
  • 2
  • You need to show how the map is being constructed and your `DataModel` class And in the `parsed on class` section, why are you putting the values in an array but printing them with their getters? – WJS Aug 28 '20 at 14:19
  • `{var_a=val_a, var_b=val_b}` looks like the EntrySet of another map. To provide an accurate answer, more information is required. – WJS Aug 28 '20 at 14:33
  • @WJS sure, i edit my question. – Budi Santoso Aug 28 '20 at 16:01
  • Thanks!. At this point I recommend you put in print statements to verify what the data looks like. I would print `rsmd.getColumnName(i)` and `rs.getObject(name)`. since those are what you are putting in the map. Printing other values may be beneficial too. It wouldn't surprise me if `name = "0"` But then, it's a guess at this point. – WJS Aug 28 '20 at 16:06
  • i put this code `System.out.println(name+"|"+rs.getObject(name));` in loop entryset map. result: `col_a|col_a col_b|col_b ...|... col_z|col_z` – Budi Santoso Aug 28 '20 at 16:15

2 Answers2

-1

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());
    }
shl_droco
  • 94
  • 3
-2

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());
    }
}
halfer
  • 19,824
  • 17
  • 99
  • 186
noNihongo
  • 178
  • 3
  • 11
  • This is not an answer but a speculation on what the OP's code did. – WJS Aug 28 '20 at 14:37
  • There is not enough information to help the OP. That is why I asked the questions in the comments. Otherwise, everyone is guessing. – WJS Aug 28 '20 at 14:40
  • I agree, but the point is here is to active his output. And it active his output. As i said we can do alot of thing to improve the code make it short, clear, etc.. But maybe he is a new developer and it could be more confuse to him. – noNihongo Aug 28 '20 at 14:41
  • You said about "speculation", there is not speculaiton. After he tried to use `map`. He said at the end `i want to get value on object like this` > `out data val_a val_b`. He wants the same output that he got before. – noNihongo Aug 28 '20 at 15:20
  • Thanks for answering. In the real case i dont want use model class, cause in real case its have many variable. – Budi Santoso Aug 28 '20 at 15:35
  • @BudiSantoso I changed the answer, i hope it fix your problem – noNihongo Aug 28 '20 at 17:30