This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface TouchCallback { | |
public void apply(int times); | |
} | |
class TouchMe { | |
private int times = 0; | |
private TouchCallback onTouch; | |
TouchMe(TouchCallback onTouch) { | |
this.onTouch = onTouch; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class LambdaExpression { | |
public static void main(String args[]) { | |
TouchMe touchMe = new TouchMe(times -> System.out.println(times)); | |
touchMe.touch(); // 1 | |
touchMe.touch(); // 2 | |
touchMe.touch(); // 3 | |
touchMe.touch(); // 4 | |
touchMe.touch(); // 5 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class LambdaExpression { | |
public static void main(String args[]) { | |
TouchMe touchMe = new TouchMe(System.out::println); | |
touchMe.touch(); // 1 | |
touchMe.touch(); // 2 | |
touchMe.touch(); // 3 | |
touchMe.touch(); // 4 | |
touchMe.touch(); // 5 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.stream.Stream; | |
public class StreamForEach { | |
public static void main(String args[]) { | |
System.out.println("Fruit List:"); | |
Stream.of("Banana", "Apple", "Kiwi") | |
.forEach(System.out::println); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.HashMap; | |
import java.util.Map; | |
public class StreamMap { | |
public static void main(String args[]) { | |
Map<String, Integer> fruits = new HashMap<>(); | |
fruits.put("Banana", 7); | |
fruits.put("Apple", 2); | |
fruits.put("Kiwi", 1); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.stream.Stream; | |
public class ReduceSum { | |
public static void main(String args[]) { | |
int sum = Stream.of(1, 2, 3, 4, 5) | |
.reduce(0, (acc, item) -> acc + item); | |
System.out.println(sum); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.HashMap; | |
import java.util.Map; | |
class Fair { | |
private final int quantity; | |
Fair(int quantity) { | |
this.quantity = quantity; | |
} |
OlderNewer