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

user interface - Python attempting to create a simple gui, getting "AttributeError: 'MainMenu' object has no attribute 'intro_screen'" -

jquery - Common JavaScript snippet to share files on Google Drive, Dropbox, Box.net or SkyDrive -

Android Gson.fromJson error -