Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Foreach loop
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== Java === In [[Java (programming language)|Java]], a foreach-construct was introduced in [[Java Development Kit]] (JDK) 1.5.0.<ref name="jdk5release"> "Enhanced for Loop - This new language construct[...]" {{cite web |url=http://java.sun.com/j2se/1.5.0/docs/guide/language/index.html |title=Java Programming Language, Section: Enhancements in JDK 5 |access-date=2009-05-26 |year=2004 |publisher=[[Sun Microsystems, Inc.]] }}</ref> Official sources use several names for the construct. It is referred to as the "Enhanced for Loop",<ref name="jdk5release"/> the "For-Each Loop",<ref> "The For-Each Loop" {{cite web |url=http://java.sun.com/j2se/1.5.0/docs/guide/language/foreach.html |title=The For-Each Loop |access-date=2009-05-10 |year=2008 |publisher=[[Sun Microsystems, Inc.]] }}</ref> and the "foreach statement".<ref> "Implementing this interface allows an object to be the target of the "foreach" statement." {{cite web |url=http://java.sun.com/javase/6/docs/api/java/lang/Iterable.html |title=Iterable (Java Platform SE 6) |access-date=2009-05-12 |year=2004 |publisher=[[Sun Microsystems, Inc.]] }}</ref><ref name=Bloch>{{cite book | title= "Effective Java: Programming Language Guide" |last=Bloch| first=Joshua| publisher=Addison-Wesley | edition=third | isbn=978-0134685991| year=2018}}</ref>{{rp|264}} <syntaxhighlight lang="Java"> for (Type item : iterableCollection) { // Do something to item } </syntaxhighlight> Java also provides the stream api since java 8:<ref name=Bloch>{{cite book | title= "Effective Java: Programming Language Guide" |last=Bloch| first=Joshua| publisher=Addison-Wesley | edition=third | isbn=978-0134685991| year=2018}}</ref>{{rp|294-203}} <syntaxhighlight lang="Java"> List<Integer> intList = List.of(1, 2, 3, 4); intList.stream().forEach(i -> System.out.println(i)); </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)