Foreach loop in java for a custom object list - Stack Overflow
use List Foreach instead of for() Java - Stack Overflow
java - Usage of For-each loop vs functional operation vs Java8 streams - Software Engineering Stack Exchange
Why does the List interface have forEach, but not map?
Videos
I read that .forEach() on a list can modify the underlying list, so I tried this:
List<Integer> nums = Arrays.asList(1, 2, 3);
nums.forEach(num -> num++);
System.out.println(nums);The printed numbers are 1, 2, 3, so nothing changed! Why not?
I understand that someStream.forEach() is based on functional programming, which doesn't allow side effects, but I still don't really grasp the difference.
Actually the enhanced for loop should look like this
for (final Room room : rooms) {
// Here your room is available
}
You can also use Java 8 stream API and do the same thing in one line.
If you want to print any specific property then use this syntax:
ArrayList<Room> rooms = new ArrayList<>();
rooms.forEach(room -> System.out.println(room.getName()));
OR
ArrayList<Room> rooms = new ArrayList<>();
rooms.forEach(room -> {
// here room is available
});
if you want to print all the properties of Java object then use this:
ArrayList<Room> rooms = new ArrayList<>();
rooms.forEach(System.out::println);
I think you can do it by iterating over the indexes of the cards:
IntStream.range(0, this.cards.size()).forEach(idx -> {
DealOneCardToPlayer(
this.table.players.get(idx % this.table.GetPlayerCount()),
this.cards.get(idx));
});
Although this doesn't remove the cards as you go; if you really need this.cards to be empty after:
this.cards.clear();
If you want to limit the number of cards dealt out (e.g. you want to deal N cards to each player), the easiest way is to extract a sublist, and then just apply the same method above:
List<Card> cardsToDeal = this.cards.subList(0, numCardsToDeal);
IntStream.range(0, cardsToDeal.size()).forEach(idx -> {
DealOneCardToPlayer(
this.table.players.get(idx % this.table.GetPlayerCount()),
cardsToDeal.get(idx));
});
cardsToDeal.clear();
You're talking about Java 8's Function API (and lambdas).
Essentially lambdas are a shorthand brethren to functions/methods, they have inputs and potentially return values. For the #forEach, it requests that you provide a function which accepts a T (Your list type), and returns nothing. This is known as a Consumer. The method then takes the Consumer you gave it, and calls it for each element on your list.
For equivalency, these are essentially the same thing as far as you're concerned when developing:
void someConsumerMethod(Card c) {
//lambda code block
}
(Card c) -> //lambda code block
this::someConsumerMethod //direct reference to the first method
An example would be:
this.cards.forEach(c -> {
System.out.println(c); //fully expanded
});
this.cards.forEach(c -> System.out.println(c)); //shorthand / one-liner
//or, since println already matches a Consumer<Object>, we can method reference it!
this.cards.forEach(System.out::println);
As for adapting your example, I wouldn't recommend modifying a collection while you iterate it (at least, not without using Iterator#remove). Andy Turner's answer already shows you how to use an application of IntStream to iterate the indexes you want.