• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

it is possible to iterate Map by using enhanced loop?

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Enthuware Question:

Which of the following are true about the enhanced for loop?

1. It can iterate over an array or a Collection but not a Map.
2. Using an enhanced for loop prevents the code from going into an infinite loop.
3. Using an enhanced for loop on an array may cause infinite loop.
4. An enhanced for loop can iterate over a Map.
5. You cannot find out the number of the current iteration while iterating.

Correct Answers is :1,2,5
My Answer is : 2,4,5

My doubt is 1 Question it possible to iterate Map or not by using enhanced loop.

but i will try this example it's fine to run.

 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You aren't iterating a Map, but a Set.
Why did you think it is impossible to go into an infinite loop? An infinite loop remains an infinite loop however you write it, and it is possible to go into an infinite loop if you manage to write a circular linked list. Ordinary arrays or linear linked lists won't go into an infinite loop if you iterate them.
 
Saloon Keeper
Posts: 27807
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can iterate any Collection. Maps are Collections, Collections are Iterable. Q.E.D.

Now some Collections may not be idempotently iterable, and unless a Collection is explicitly defined as being an Ordered Collection, the sequence in which the elements may iterate can be unpredictable, but you can nonetheless iterate them.

Furthermore, in the case of a Map, the Entries as key/value pairs may be iterated, as well as the keySet and EntrySet Collections defined within the Map.
 
Bartender
Posts: 206
14
Mac OS X IntelliJ IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:Maps are Collections



Are you sure?
 
Tim Holloway
Saloon Keeper
Posts: 27807
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, it seems that in Java, a Map is a container for Collections, rather than a direct implementation or descendant of Collection itself. So literally speaking, a Map isn't itself iterable. But its aspects are.
 
Marshal
Posts: 4510
572
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Map is part of the Collections Framework, and like Tim mentioned, does provide iterable views for the keys, values, and entries.

Javadoc for Interface Map<K,V> wrote:The Map interface provides three collection views, which allow a map's contents to be viewed as a set of keys, collection of values, or set of key-value mappings. The order of a map is defined as the order in which the iterators on the map's collection views return their elements. Some map implementations, like the TreeMap class, make specific guarantees as to their order; others, like the HashMap class, do not.

 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But Map itself isn't Iterable, which it probably why question 1 was shown as right.
 
Tim Holloway
Saloon Keeper
Posts: 27807
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Technically the truth, but have you ever carelessly said "I'll iterate over this Map" as a shorthand for walking the keys or values?

Actually, an occasional question I've dealt with in the JSF forum has been when people tried to use a Map as a Model object and I had to explain to them that maps were not iterable because they were not ordered collections. I'm not entirely sure but what EL doesn't find something to iterate when the item in question is a Map, just not something that's got a guaranteed order.

As is so often the case, the "right" answers on exams are frequently determined by what context you consider the questions under.

Also, question 4 seems redundant after question 1.
 
Enthuware Software Support
Posts: 4818
52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As per Section 14.4.2 of JLS 8, "The type of the Expression must be Iterable or an array type (ยง10.1), or a compile-time error occurs.". Map does not implement Iterable and is not a Collection either. (Yes, it is part of the Collections Framework but is not a Collection itself).
Therefore, the given answer is correct.
Option 1 and option 4 may look the same but are actually different.

BTW, the following explanation is already given with option 1:


An enhanced for loop needs either an array or an object of a class that implements java.lang.Iterable. Map does not implement Iterable, though you can use keySet() or values() methods to get a Collection (which extends Iterable) and then iterate over that Collection.

 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:. . . have you ever carelessly said "I'll iterate over this Map" . . .? . . . .

Of course we have; we say all sorts of things carelessly. I once carelessly thought to myself, “I'll put some water from the kettle on my coffee,” and just as carelessly didn't put the kettle on first. Fortunately cold coffee still tastes like coffee and I could microwave it. Tea would have tasted like water
 
Tim Holloway
Saloon Keeper
Posts: 27807
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It took me a minute to figure out what sort of preparation method you were using until I realized it's probably pour-over.  I'm too used to machines or pots.

But the analogy was inexact, since when you got done, you only had "partial" coffee, but when I iterate a Map, the entire Map is indeed iterated. It's only that the details of the process are implicit.
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pour‑over with a nylon filter.
 
reply
    Bookmark Topic Watch Topic
  • New Topic