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
String carModels = cars.parallelStream() //go parallel | |
.map(c1 -> c1.getModel()) | |
.distinct() | |
.reduce("Renault", (s1, s2) -> s1 + "|" + s2); | |
System.out.println(carModels); | |
//Output: Renault|BMW|Renault|Ford|Renault|Toyota|Renault |
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
//append all model into single line of string with default model: Renault | |
String carModels = cars.stream() | |
.map(c1 -> c1.getModel()) | |
.distinct() | |
.reduce("Renault", (s1, s2) -> s1 + "|" + s2); | |
System.out.println(carModels); | |
//Output: Renault|BMW|Ford|Toyota |
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
Car car1 = new Car("BMW", "Red", 35000d); | |
Car car2 = new Car("Ford", "Yellow", 25000d); | |
Car car3 = new Car("Toyota", "Red", 10000d); | |
Car car4 = new Car("BMW", "Green", 35500d); | |
List<Car> cars = Arrays.asList(car1, car2, car3, car4); | |
//append all model into single line of string | |
cars.stream() | |
.map(c1 -> c1.getModel()) |
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
// list of negative number | |
List<Integer> negList = Arrays.asList(-4, -3, -2, -1); | |
// list of positive number | |
List<Integer> posList = Arrays.asList(1, 2, 3, 4); | |
// find the odd number in both lists with single stream operation | |
List<Integer> collector = Stream.of(negList, posList) | |
.flatMap(List::stream) //transform into stream of other objects | |
.filter(i -> i % 2 != 0) |
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
//there is at least one negative number | |
public void anyMatch(){ | |
boolean anyMatch; | |
anyMatch = IntStream.of(-1,2,3,5,7,9) | |
.anyMatch(i -> i < 0); | |
System.out.format("It is [%b] that at least one negative number \n", | |
anyMatch); | |
//Output: It is [true] that at least one negative number |
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
Car car1 = new Car("BMW","Red", 35000d); | |
Car car2 = new Car("BMW","Green", 35500d); | |
Car car3 = new Car("Ford","Yellow", 25000d); | |
Car car4 = new Car("Toyota","Red", 10000d); | |
//populate car model list whose have red values | |
List<String> redCars = Stream.of(car1, car2, car3, car4) | |
.filter(s -> s.getColor().equals("Red") ) //get all cars with red values | |
.map(c -> c.getModel()) //map to get the model only | |
.collect(Collectors.toList()); |
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 Car { | |
private String model; | |
private String color; | |
private Double price; | |
public Car(String model, String color, Double price) { | |
this.model = model; | |
this.color = color; | |
this.price = price; |
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
list.stream() | |
.filter(i -> i % 2 == 0) | |
.map(i -> i * i) | |
.sorted() | |
.distinct() | |
.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
Random random = new Random(); | |
List<Integer> list = new ArrayList<>(); | |
//create a list of random integers using stream. | |
//Don't have to be like this, just to show off | |
//because we are talking about stream | |
List<Integer> list = IntStream.range(0, 20) | |
.map(i -> random.nextInt(100)) | |
.boxed() //turns IntStream into Stream<Integer> | |
.collect(Collectors.toList()); //will be discussed in advance stream usage |
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
//Stream from Collection | |
List<String> list = new ArrayList<>(); | |
list.add("s1"); | |
list.add("s2"); | |
list.add("s3"); | |
list.stream() | |
.map( s -> "From collection " + s) | |
.forEach(System.out::println); | |
//Stream from Arrays class |