vendredi 26 juin 2015

looping Nested If else in a TreeMap logic

I'm putting values in a TreeMap as 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 to retrieve it 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 the output I want is if any key is not found it should go in an else part (which I have not included here , as Im not getting the desired output) and it should append

               stringBuilder.append(,,);

for example

        If key 1 contains 12,13
        If key 2 is not present ,,
        If key 3 contains 10,11
        If key 4 contains 14,15

So the desired outout should be

       12,13,,10,11,14,15

Kindly help me with logic, as Im breaking my head over this.

Thanks

Aucun commentaire:

Enregistrer un commentaire