I used to use for honestly so far, so make a note.
List format data obtained from DB I used it when I wanted to filter it and convert it to Map format (which was easier when displaying it with jsp).
List<myClass> resList = myClassDao.query(insMap);
//filtering
List<myClass> dataList = resList.stream()
.filter(c -> c.getHogeId() != null)
.collect(Collectors.toList());
//Convert to Map
Map<Integer, List<myClass>> res = dataList.stream().collect(
Collectors.groupingBy(myClass::getHogeId)
);
With this, the data like this
[
{
id:1,
hogeId:1,
data:"a"
},
{
id:2,
hogeId:1,
data:"b"
},
{
id:3,
hogeId:2,
data:"c"
},
{
id:4,
hogeId:null,
data:"d"
}
]
It will be like this. Perhaps.
{
1:[
{
id:1,
hogeId:1,
data:"a"
},
{
id:2,
hogeId:1,
data:"b"
}
],
2:[
{
id:3,
hogeId:2,
data:"c"
}
]
}
Please let me know if there is a better way!
Recommended Posts