How to iterate over a nested list
Hi I have a hibernate query which is giving me a list with type List<List<integer>>
.
How can I iterate this? My hibernate query is:
String SQL_QUERY = "select DISTINCT cbl.franchiseId,cbl.resellerId from
CustomerBusinessLocation cbl where cbl.cmcustLocId in (:locationId)";
Query query =开发者_运维百科 getSession().createQuery(SQL_QUERY);
query.setParameterList("locationId", customerLocId);
List<List<Integer>> rc_list = query.list();
Alternatively, is there any other way to easily pull in this data?
This query doesn't return a List<List<Integer>>
. It returns a List<Object[]>
. The Object[]
arrays contain one element per requested field. In your case, each Object[]
will contain franchiseId
at index 0 and resellerId
at index 1.
This is of course explained in the reference documentation.
The iteration should thus look like this:
List<Object[]> rows = query.list();
for (Object[] row : rows) {
Integer franchiseId = (Integer) row[0];
Integer resellerId = (Integer) row[1];
// ...
}
To iterate nested lists, you simply nest for loops.
for(List<Integer> list : rc_list){
for(Integer i : list){
//do stuff
}
}
精彩评论