0

I am using HashMap and have found an example of using compact type of FOR

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

hashMap.put("one", 5);
    hashMap.put("two", 8);
    hashMap.put("three", 12);
    hashMap.put("four", 5);

    Set<Map.Entry<String, Integer>> set = hashMap.entrySet();

    for (Map.Entry<String, Integer> me : set) {
    System.out.print(me.getKey() + ": ");
    System.out.println(me.getValue());
    }

And i cant find an answer how does people use short variant of writing cycle FOR and how do they generate parameters for that. I mean this line

for (Map.Entry<String, Integer> me : set)
Bo Z
  • 2,359
  • 1
  • 13
  • 31

1 Answers1

0

When we use short type of cycle For in example

String[] fruits = new String[] { "Orange", "Apple", "Pear", "Strawberry" };

    for (String fruit : fruits) {
    // fruit is an element of the `fruits` array.
    }

String fruit - is variable ( on the left of ":" )

fruits is an object where all our elements saved ( on the right of ":" )

Bo Z
  • 2,359
  • 1
  • 13
  • 31