0

I am a Javascript developer and I starting to check out Java.

One question that I have is how I can perform forEach on a collection. I can't seem to find any such method in Java collections...

DimitrisBor
  • 297
  • 3
  • 12
  • 1
    http://docs.oracle.com/javase/1.5.0/docs/guide/language/foreach.html, http://stackoverflow.com/questions/85190/how-does-the-java-for-each-loop-work These links helps you – Vinay Veluri Mar 24 '14 at 10:56
  • 1
    @devnull - Maybe because there exisits no `method` for this but only syntatic iteration in form of a for. A `method` would be something like: collection.ForEach(//do something in here...); – Rand Random Mar 24 '14 at 11:02

2 Answers2

5

Since Java 5, you can use the enhanced for loop for this. Assume you have (say) a List<Thingy> in the variable thingies. The enhanced for loop looks like this:

for (Thingy t : thingies) {
    // ...
}

As of Java 8, there's an actual forEach method on iterables that accepts a lambda function:

thingies.forEach((Thingy t) -> { /* ...your code here... */ });

If you have an existing method you want to use, you can use a method reference instead of an inline lambda:

thingies.forEach(this::someMethod);      // Instance method on `this`
thingies.forEach(SomeClass::someMethod); // Static method on `SomeClass`
thingies.forEach(foo::someMethod);       // Instance method on `foo` instance
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
1

What you are trying to accomplish can only be done in a form similar to JavaScript, in Java 8 or newer. Java 8 added the forEach (defender) method on Iterable (for which Collection inherits)

geoand
  • 60,071
  • 24
  • 172
  • 190
  • 1
    Strange answer, you are implying he cannot do a for each loop similar to JavaScript in Java < 8? – Daniël Knippers Mar 24 '14 at 10:58
  • 1
    The user obviously does not want a for loop but a forEach method on a collection – geoand Mar 24 '14 at 11:00
  • The *enhanced* `for` loop (`for (Thingy t : collectionOfT)`) was added to [**Java 5**](http://docs.oracle.com/javase/specs/jls/se5.0/html/statements.html#14.14.2) (e.g., many years ago), not Java 8. – T.J. Crowder Mar 24 '14 at 11:01
  • Again, I am not talking about for(Integer obj : myCollection) {}, I am talking about myCollection.forEach(); – geoand Mar 24 '14 at 11:03
  • @geoand: JavaScript has the `for-in` statement, which is very like the enhanced `for` statement. I think you're being a bit over-literal with the OP referencing `forEach`. But yes, collections only got the lambda version recently. – T.J. Crowder Mar 24 '14 at 11:04
  • @T.J.Crowder I see where you are coming from, but my understanding of the question was that the asker had code like var a = ["a", "b", "c"]; a.forEach(function(entry) { console.log(entry); }); and wanted to translate it to a Java collection without the for loop – geoand Mar 24 '14 at 11:06
  • 1
    @T.J.Crowder Since OP is explicitly asking for a *`forEach` method of the `Collection` interface*, I think this answer fits best. The enhanced for loop also deserves a mention, but this answer fits the question squarely. – Marko Topolnik Mar 24 '14 at 11:06
  • 1
    He is asking how to *perform forEach on a collection*, i.e. he is asking how to iterate a collection. Not necessarily a literal method by the name of `forEach`. That would be my interpretation. – Daniël Knippers Mar 24 '14 at 11:07
  • @DaniëlKnippers How about "I can't find any such method in Java collections"? – Marko Topolnik Mar 24 '14 at 11:08