consumidor
@FunctionalInterface
public interface Consumer<T> {
void accept(T t);
}
Xerothermic Xenomorph
@FunctionalInterface
public interface Consumer<T> {
void accept(T t);
}
package com.zetcode;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
public class ConsumerEx4 {
public static void main(String[] args) {
var vals = new ArrayList<Integer>();
vals.add(2);
vals.add(4);
vals.add(6);
vals.add(8);
Consumer<List<Integer>> addTwo = list -> {
for (int i = 0; i < list.size(); i++) {
list.set(i, 2 + list.get(i));
}
};
Consumer<List<Integer>> showList = list ->
list.forEach(System.out::println);
addTwo.andThen(showList).accept(vals);
}
}
package com.zetcode;
import java.util.function.Consumer;
import java.util.function.IntConsumer;
public class ConsumerEx2 {
public static void main(String[] args) {
Consumer<Integer> printMultiplyBy100 = (val) -> System.out.println(val * 100);
printMultiplyBy100.accept(3);
printMultiplyBy100.accept(4);
printMultiplyBy100.accept(5);
IntConsumer printMultiplyBy500 = a -> System.out.println(a * 50);
printMultiplyBy500.accept(1);
printMultiplyBy500.accept(2);
printMultiplyBy500.accept(3);
}
}
package com.zetcode;
import java.util.List;
import java.util.function.Consumer;
public class ConsumerEx3 {
public static void main(String[] args) {
var words = List.of("falcon", "wood", "rock", "forest",
"river", "water");
words.forEach(new Consumer<String>() {
@Override
public void accept(String s) {
System.out.println(s);
}
});
}
}