简体   繁体   中英

Why is the “else” code block executed for this for loop?

I think I understand the first statement. It's saying, if the items in "words" are NOT in "frequencies", then add them and assign a value of 1 to each, right? What I am more confused about is why the "else" block gets executed and how it works. I understand the output, but not quite how it works. It obviously recognizes that a particular word appears more than once but how does it recognize that? And again, why does it go to the "else" block if the first statement is true?

public class Testing {

  static List<String> list() {

    List<String> words = new ArrayList<String>();

    words.add("Cherry");
    words.add("Banana");
    words.add("Apple");
    words.add("Banana");
    words.add("Berry");

    return words;
  }

  static Map<String, Integer> ArrayFrequencies(List<String> words) {

    Map<String, Integer> frequencies = new HashMap<String, Integer>();

    for (String elements : words) {

      if (!frequencies.containsKey(elements)) {
        frequencies.put(elements, 1);
      } else {
        frequencies.put(elements, frequencies.get(elements) + 1);
      }
    }

    return frequencies;
  }

  public static void main(String[] args) {

    System.out.println(ArrayFrequencies(list()));
  }
}

output : {Apple=1, Cherry=1, Berry=1, Banana=2}

  if(!frequencies.containsKey(elements))  
    {
        //put the data in map for the first time
        //Suppose Element "Apple" entering for the first time
        frequencies.put(elements, 1); 
    } else {
       //put the data in map if map already contains that element
       //Element "Apple entering for the second time". Here it will get previous count and increase by one
       frequencies.put(elements, frequencies.get(elements) + 1 );
    }

What I am more confused about is why the "else" block gets executed and how it works.

The if block sets the map entry to 1 explicitly because it's the first value being associated with the elements key.

The else block is updating the map entry. It exists, it has a value, and it needs to be incremented. So you say:

frequencies.get(elements) + 1 //get the prior value and add one to it
frequencies.put(^^^^^^^)      //update the map entry with the new value from above.

NOTE : You can't apply the logic from the else block to the if, because you can't increment null.

Your code is behaving as expected. The else statement is only executed when it needs to(When a given item is already in the list and you only need to increment its count). Here, I wrote some debug messages to your code. Run it and you will see exactly what is going on.

package strings;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Testing {

  static List<String> list() {

    List<String> words = new ArrayList<String>();

    words.add("Cherry");
    words.add("Banana");
    words.add("Apple");
    words.add("Banana");
    words.add("Berry");
    words.add("Banana");
    words.add("Berry");
    words.add("Banana");

    return words;
  }

  static Map<String, Integer> ArrayFrequencies(List<String> words) {

    Map<String, Integer> frequencies = new HashMap<String, Integer>();

    for (String element : words) {

      if (!frequencies.containsKey(element)) {
            System.out.println("Seems like => " + element + "  is not in the list yet. Adding it" );
        frequencies.put(element, 1);
      } else {

           System.out.println("Seems like => " + element + " is in the list already. Incrementing its count from  : " + 
                              frequencies.get(element) + "  => to   : " + (frequencies.get(element) + 1) );
        frequencies.put(element, frequencies.get(element) + 1);
      }
    }

    return frequencies;
  }

  public static void main(String[] args) {

    System.out.println(ArrayFrequencies(list()));
  }
}

Outputs:

Seems like => Cherry  is not in the list yet. Adding it
Seems like => Banana  is not in the list yet. Adding it
Seems like => Apple  is not in the list yet. Adding it
Seems like => Banana is in the list already. Incrementing its count from  : 1  => to   : 2
Seems like => Berry  is not in the list yet. Adding it
Seems like => Banana is in the list already. Incrementing its count from  : 2  => to   : 3
Seems like => Berry is in the list already. Incrementing its count from  : 1  => to   : 2
Seems like => Banana is in the list already. Incrementing its count from  : 3  => to   : 4
{Apple=1, Cherry=1, Berry=2, Banana=4}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM