java - looping Nested If else in a TreeMap logic -
i'm putting values in treemap
follows:
if (hdr.getname().equalsignorecase("ip4")) { map.put(1, f); } else if (hdr.getname().equalsignorecase("ethernet")) { map.put(2, f); } else if (hdr.getname().equalsignorecase("tcp")) { map.put(3, f); } else if (hdr.getname().equalsignorecase("udp")) { map.put(4, f); }
and i'm trying retrieve using:
iterator < map.entry < integer, field >> entries = map2.entryset().iterator(); while (entries.hasnext()) { map.entry < integer, field > entry = entries.next(); if ((entry.getkey() == 1)) { stringbuilder.append(entry.getvalue().getsource()); stringbuilder.append(","); stringbuilder.append(entry.getvalue().getdestination()); } else if ((entry.getkey() == 2)) { stringbuilder.append(entry.getvalue().getsource()); stringbuilder.append(","); stringbuilder.append(entry.getvalue().getdestination()); } else if ((entry.getkey() == 3)) { stringbuilder.append(entry.getvalue().getsource()); stringbuilder.append(","); stringbuilder.append(entry.getvalue().getdestination()); } else if ((entry.getkey() == 4)) { stringbuilder.append(entry.getvalue().getsource()); stringbuilder.append(","); stringbuilder.append(entry.getvalue().getdestination()); } }
now, output want if key not found, should go in else part (which have not included here not getting desired output) , should append
stringbuilder.append(,,);
for example
if key 1 contains 12,13 if key 2 not present ,, if key 3 contains 10,11 if key 4 contains 14,15
so desired outout should
12,13,,10,11,14,15
kindly me logic breaking head on this.
thanks
thanks hint david , solved using
(int i=1 ; i<=map2.size()+1; i++){ if(map2.containskey(i)) { //appendstring(map2.get(i)); stringbuilder.append(map2.get(i).getsource()); stringbuilder.append(","); stringbuilder.append(map2.get(i).getdestination()); } else{ stringbuilder.append(",,"); } }
Comments
Post a Comment