Skip to content

Instantly share code, notes, and snippets.

interface TouchCallback {
public void apply(int times);
}
class TouchMe {
private int times = 0;
private TouchCallback onTouch;
TouchMe(TouchCallback onTouch) {
this.onTouch = onTouch;
public class Sum {
public static void main(String args[]) {
int[] array = new int[]{1, 2, 3, 4, 5};
int sum = 0;
for (int item: array) {
sum += item;
}
System.out.println(sum);
import java.util.Arrays;
public class StreamForEach {
public static void main(String args[]) {
int[] array = new int[]{1, 2, 3, 4, 5};
Arrays.stream(array)
.forEach(item -> System.out.println("item: " + item));
}
}
import java.util.Arrays;
public class StreamSum {
public static void main(String args[]) {
int[] array = new int[]{1, 2, 3, 4, 5};
int sum = Arrays.stream(array).sum();
System.out.println(sum);
}
}