java - List to Hashmap to create API -


i have method return object , count, due following sql request

public list<sales> mostloyal() {     string querystring = "select s, count(s.saleid) totalnum sales s group s.custid.custlastname order totalnum desc";     list<sales> breakdown = em.createquery(querystring).setmaxresults(10).getresultlist();     return breakdown; } 

i'm trying transform list hashmap containing object (s in request) , count. i've tried lot of things can't figure life of me out how that.
goal return hashmap json.

you can iterate through list , place values in hashmap , return it. should simple.

public map<string,integer> mostloyal() {     map<string,integer> m = new hashmap<string,integer>();     string querystring = "select s, count(s.saleid) totalnum sales s group s.custid.custlastname order totalnum desc";     list<sales> breakdown = em.createquery(querystring).setmaxresults(10).getresultlist();      for(sales sale: breakdown){       m.put(sale.gets(),sale.getcount());      }     return m; } 

Comments

Popular posts from this blog

python - How to create a legend for 3D bar in matplotlib? -

java - Multi-Label Document Classification -

php - Dynamic url re-writing using htaccess -