Loading...
Saturday, March 31, 2018

Stream api takewhile() method in Java9


StreamAPITakewhile.java
 package com.java9r;

import java.util.stream.Stream;

/**
* @author Ravi
*
*/
public class StreamAPITakewhile {

/**
* @param args
*/
public static void main(String[] args) {

System.out.println("\nProducts List");

System.out.println("=========================");
Product product1 = new Product("001", "product 1", 100);
Product product2 = new Product("002", "product 2", 200);
Product product3 = new Product("003", "product 3", 300);
Product product4 = new Product("004", "product 4", 400);
Product product5 = new Product("005", "product 5", 500);
Product product6 = new Product("006", "product 6", 600);
Product product7 = new Product("007", "product 7", 700);
Product product8 = new Product("008", "product 8", 800);
Product product9 = new Product("009", "product 9", 900);
//default Stream<T> takeWhile(Predicate<? super T> predicate)

Stream.of(product1, product2, product3, product4, product5, product6, product7, product8, product9)
.takeWhile(p -> p.getPrice() <= 700).forEach(p -> {
//takeWhile is similar to filter in the sense that it expects a predicate and returns a new stream
//takeWhile takes elements from the initial stream while the predicate holds true
//an element is encountered that does not match the predicate, the rest of the stream is discarded.
System.out.println("Id: " + p.getId());
System.out.println("Name: " + p.getName());
System.out.println("Price: " + p.getPrice());
System.out.println("=========================");
});

}

}



Product.java
 package com.java9r;

public class Product {

private String id;
private String name;
private double price;

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public double getPrice() {
return price;
}

public void setPrice(double price) {
this.price = price;
}

public Product(String id, String name, double price) {
this.id = id;
this.name = name;
this.price = price;
}


}



Output
 Products List
=========================
Id: 001
Name: product 1
Price: 100.0
=========================
Id: 002
Name: product 2
Price: 200.0
=========================
Id: 003
Name: product 3
Price: 300.0
=========================
Id: 004
Name: product 4
Price: 400.0
=========================
Id: 005
Name: product 5
Price: 500.0
=========================
Id: 006
Name: product 6
Price: 600.0
=========================
Id: 007
Name: product 7
Price: 700.0
=========================



2 comments:

  1. Casino Site | LuckyClub | Play with friends | Live Casino
    Welcome to the LuckyClub Casino website. Register and play with friends for luckyclub instant deposits and win real money. No matter where you are, you can win real money with

    ReplyDelete
  2. Casino Review 2021 | MrDMD
    We 경기도 출장마사지 found that this casino has the 구리 출장안마 best 전라북도 출장샵 welcome bonus, promotions, games 의정부 출장샵 and mobile app 창원 출장샵 ranking. This casino is an instant download  Rating: 4.2 · ‎Review by drmcd

    ReplyDelete

 
TOP