• 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

what is difference between foreach and for?

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what is difference between foreach and for?
Which gives better perfomance?
please give reply ........
 
Ranch Hand
Posts: 344
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The syntax. Not sure if there's a performance difference.
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you read the Java Tutorials?
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

veera sangham wrote:what is difference between foreach and for?
Which gives better perfomance?
please give reply ........



There is no performance difference. A foreach loop is simply syntactic sugar.

For Iterables, the compiler turns this:


into this (or an equivalent):


Similarly, for arrays, this:


becomes this:

 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note that the for-each loop operates on a copy of the element. So something like... will print Campbell several times, whilst leaving the original array or collection unchanged.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Note that the for-each loop operates on a copy of the element. So something like... will print Campbell several times, whilst leaving the original array or collection unchanged.



Which is exactly the same as the common idiom that it's syntactic sugar for.



This too will print Campbell several times, but will not affect the original collection. Same goes for arrays. In both foreach and the standard idiom from before foreach existed, we are operating with a local variable that is a copy of the value held in the collection or array, and modifying that local variable does not affect the collection or array.
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Might as well reinforce that lesson; we have all tried to assign to that local variable and wondered why it had no effect.
 
Master Rancher
Posts: 4796
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, the reference is copied - the object it refers to is not. If you've got mutable objects in the collection, you can certainly alter them inside a for loop.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Simmons wrote:Well, the reference is copied



Right, that's what we're saying.

If you've got mutable objects in the collection, you can certainly alter them inside a for loop.



Right. But we're talking about altering a variable. And just like everywhere else in Java, altering one variable doesn't affect any other variables.
 
Mike Simmons
Master Rancher
Posts: 4796
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, I posted amidst other distractions at work, and had only seen the original comment from Campbell. I was only concerned with the ambiguity about what "copy" he was referring to, which was quite vague at the time. It sounded like he might be talking about a deep copy, rather than a basic fact of how references work in Java, with nothing specifically to do with foreach. Oh well.
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was a bit vague there. Sorry.
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
for-each is a Java 5 feature.
Java 5 provided Interface Iterable. Those collections implements/extends this inteface is allowed to use for-each loop.
Maps don't extends Iterable so not allowed to use for-each directly.
All other Collection classes can use it.

About performace can't comment.
 
veera sangham
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for giving the quick reply
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Iterable<E> interface was introduced in Java5 along with the for-each loop; the two support each other. Any collections class which supported the iterator() method was retrofitted to implement the Iterable<E> interface. The way this was done was to change the Collection<E> interface to Collection<E> extends Iterable<E>. Since interfaces like Set<T> and List<T> already extend Collection<T>, this means any List<E> or Set<E>, etc. is also an Iterable<E>. The iterator() method was put into the Iterable<E> interface (I can’t remember whether it is removed from Collection<E> or not).
Maps<K, V> have never implemented an iterator() method, because you never go through a Map<K, V>. You go through its Ks or its Vs, so you can get them as a Set<K> or a Set<V>, which you can traverse with a for-each loop, since th/ose are now Iterable<E> objects.
 
veera sangham
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:The Iterable<E> interface was introduced in Java5 along with the for-each loop; the two support each other. Any collections class which supported the iterator() method was retrofitted to implement the Iterable<E> interface. The way this was done was to change the Collection<E> interface to Collection<E> extends Iterable<E>. Since interfaces like Set<T> and List<T> already extend Collection<T>, this means any List<E> or Set<E>, etc. is also an Iterable<E>. The iterator() method was put into the Iterable<E> interface (I can’t remember whether it is removed from Collection<E> or not).
Maps<K, V> have never implemented an iterator() method, because you never go through a Map<K, V>. You go through its Ks or its Vs, so you can get them as a Set<K> or a Set<V>, which you can traverse with a for-each loop, since th/ose are now Iterable<E> objects.





Thank you Campbell
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:
Maps<K, V> have never implemented an iterator() method, because you never go through a Map<K, V>. You go through its Ks or its Vs, so you can get them as a Set<K> or a Set<V>...


Or as a Set<Map.Entry<K, V>> (via the entrySet() method), which is the most efficient approach if you need to iterate through the map accessing both the keys and the values.
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:You go through its Ks or its Vs, so you can get them as a Set<K> or a Set<V>, which you can traverse with a for-each loop, since th/ose are now Iterable<E> objects.


You can get a Set<K>, Collection<V> (not Set<V>) and also a Set<Map.Entry<K,V>>. The latter allows you to iterate through the keys and values at the same time.

Map could have been designed from the start to have an iterator() method that would return the same as entrySet().iterator(). However, because there are many Map implementations around that don't have this method it's impossible to add it now. Doing so would break all of these implementations.
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, I forgot about the set of entries. And was mistaken about the values being a set. Sorry. As you said, Rob, it is conceivable that Maps could have been designed with Iterators, but it is too late now. some older Maps could give you an Enumeration, but that is slightly different from an Iterator. Only slightly, but enough that you can’t call a Map an Iterable.
 
Maybe he went home and went to bed. And took this tiny ad with him:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic