[英]List to Hashmap to create API
I have some method that return an object and a count, due to the following SQL request 由于以下SQL请求,我有一些返回对象和计数的方法
public List<Sales> mostLoyal() {
String queryString = "SELECT s, COUNT(s.saleId) as TotalNum FROM Sales s GROUP BY s.custId.custLastName ORDER BY TotalNum DESC";
List<Sales> breakdown = em.createQuery(queryString).setMaxResults(10).getResultList();
return breakdown;
}
I'm trying to transform this List into a Hashmap containing the object (s in the request) and the count. 我正在尝试将此列表转换为包含对象(请求中的s)和计数的Hashmap。 I've tried a lot of things but I can't figure for the life of me out how to do that. 我已经尝试了很多事情,但是我无法弄清楚如何做到这一点。
The goal is to return this Hashmap into a json. 目标是将此Hashmap返回给json。
You can simply iterate through your list and place values in HashMap and return it. 您可以简单地遍历列表并将值放入HashMap中并返回。 Should be simple. 应该很简单。
public Map<String,Integer> mostLoyal() {
Map<String,Integer> m = new HashMap<String,Integer>();
String queryString = "SELECT s, COUNT(s.saleId) as TotalNum FROM Sales s GROUP BY s.custId.custLastName ORDER BY TotalNum DESC";
List<Sales> breakdown = em.createQuery(queryString).setMaxResults(10).getResultList();
for(Sales sale: breakdown){
m.put(sale.getS(),sale.getCount());
}
return m;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.