DISABLE ADBLOCK

ADBlock is blocking some content on the site

ADBlock errore

How to get values from a list which is generated by "new map()" HQL?

StackOverflow https://stackoverflow.com/questions/14276289

Question

This is my HQL.

List<Map<Long, String>> list = getSession().createQuery("select new map(id, fname) from Employee").list();

When I print the list variable on console, I get

{1=Kevin, 0=5}, {1=Louis, 0=8}, {1=Micheal, 0=15}

I just want to know how to iterate the above list and how to get values from the list.

Please help. Thanks in advance.

Solution

Without knowing the types involved it'll be something like this:

for (Map.Entry entry : List) {
  System.out.println(entry.getKey());
  System.out.println(entry.getValue());
}

Update

Based on the new sample the answer is this:

for (Map<Long, String> map : list) {
  for (Entry<Long, String> entry : map.entrySet()) {
    System.out.println(entry.getKey());
    System.out.println(entry.getValue());
  }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow