java - Compare and return HashMap for given value -
java - Compare and return HashMap for given value -
i have hashmap below :
somemap= new hashmap<integer, string>(); somemap.put(1, "variable1"); somemap.put(2, "variable2"); somemap.put(3, "variable3"); somemap.put(4, "variable4"); somemap.put(5, "variable5");
and have java class below :
public class somevo { long somenumber; string shortdesc; public long getsomenumber() { homecoming somenumber; } public void setsomenumber(long somenumber) { this.somenumber = somenumber; } public string getshortdesc() { homecoming shortdesc; } public void setshortdesc(string shortdesc) { this.shortdesc = shortdesc; } }
in database have values
somenumber , short-description
when query database homecoming list have above info :
list<somevo > existingsomenumberandshortdescriptionlist
now have compare list
withe somemap
, homecoming 2 maps have variable key , short description variable.
like have compare existingsomenumberandshortdescriptionlist
, need have final results like
variable1, shortdescription(this database available in existingsomenumberandshortdescriptionlist),
and variable1, y or n if somenumber available in list y
else n
your code :
public class test { /** * @param args */ public static void main(string[] args) { // loaded hashmap------------------------------------------------------------------- hashmap<long, string> somemap= new hashmap<long, string>(); somemap.put(1l, "variable1"); somemap.put(2l, "variable2"); somemap.put(3l, "variable3"); somemap.put(4l, "variable4"); somemap.put(5l, "variable5"); // list getting db------------------------------------------------------------------- list<somevo> existingsomenumberandshortdescriptionlist = new arraylist<somevo>(); somevo somevo1=new somevo(); somevo1.setsomenumber(1l); somevo1.setshortdesc("description 1"); somevo somevo2=new somevo(); somevo2.setsomenumber(2l); somevo2.setshortdesc("description 2"); existingsomenumberandshortdescriptionlist.add(somevo1); existingsomenumberandshortdescriptionlist.add(somevo2); //-------------------------------------------------------------------------------------------- hashmap<string, string> hashmap1 =new hashmap<string, string>(); hashmap<long, string> hashmap2 =new hashmap<long, string>(); //iterate through list of bean (iterator<somevo> iterator = existingsomenumberandshortdescriptionlist .iterator(); iterator.hasnext();) { somevo somevo = (somevo) iterator.next(); // compare key main hashmap , set in hashmap 1 hashmap1.put(somemap.get(somevo.getsomenumber()),somevo.getshortdesc()); // compare key main hashmap , check if number exists , set in hashmap 2 if(somemap.containskey(somevo.getsomenumber())) hashmap2.put(somevo.getsomenumber(),"y"); else hashmap2.put(somevo.getsomenumber(),"n"); } // print hashmaps system.out.println(hashmap1); system.out.println(hashmap2); }
and output be..
{variable1=description 1, variable2=description 2} {1=y, 2=y}
java arraylist hashmap
Comments
Post a Comment