英文:
Comparing for a given value in a list of objects - java 8
问题
import java.util.List;
import java.util.stream.Collectors;
class Product {
String productName;
int mfgYear;
int expYear;
}
int testYear = 2019;
List<Product> productList = getProductList();
List<String> productNameList = productList.stream()
.filter(product -> product.mfgYear <= testYear && testYear <= product.expYear)
.map(product -> product.productName)
.collect(Collectors.toList());
英文:
I have list of Objects <Product>
class Product{
String productName;
int mfgYear;
int expYear;
}
int testYear = 2019;
List<Product> productList = getProductList();
I have list of products here.
Have to iterate each one of the Product from the list and get the List<String> productName
that lies in the range between mfgYear & expYear for a given 2019(testYear).
For example,
mfgYear <= 2019 <= expYear
How can I write this in java 8 streams.
答案1
得分: 1
int givenYear = 2019;
List<String> productNames =
products.stream()
.filter(p -> p.mfgYear <= givenYear && givenYear <= p.expYear)
.map(Product::name)
.collect(Collectors.toList());
// It would be more clean if you can define a boolean function inside your product class
class Product {
// your code as it is
boolean hasValidRangeGiven(int testYear) {
return mfgDate <= testYear && testYear <= expYear;
}
}
List<String> productNames = products.stream()
.filter(p -> p.hasValidRange(givenYear))
.map(Product::name)
.collect(Collectors.toList());
英文:
You can write as following:
int givenYear = 2019;
List<String> productNames =
products.stream()
.filter(p -> p.mfgYear <= givenYear && givenYear <= p.expYear)
.map(Product::name)
.collect(Collectors.toList());
// It would be more clean if you can define a boolean function inside your product class
class Product {
// your code as it is
boolean hasValidRangeGiven(int testYear) {
return mfgDate <= testYear && testYear <= expYear:
}
List<String> productNames = products.stream()
.filter(p -> p.hasValidRange(givenYear))
.map(Product::name)
.collect(Collectors.toList());
答案2
得分: 0
List<String> process(List<Product> productList) {
return productList.stream()
.filter(this::isWithinRange)
.map(Product::getProductName)
.collect(Collectors.toList());
}
boolean isWithinRange(Product product) {
return product.mfgYear <= 2019 && product.expYear <= 2019;
}
static class Product {
String productName;
int mfgYear;
int expYear;
public String getProductName() {
return productName;
}
}
**filter()**会通过lambda表达式(或在此情况下是方法引用)返回true的任何项目。**map()**将传递项目给方法引用,并创建其返回类型的流。在这种情况下,我们传递了名称获取器。
英文:
List<String> process(List<Product> productList) {
return productList.stream()
.filter(this::isWithinRange)
.map(Product::getProductName)
.collect(Collectors.toList());
}
boolean isWithinRange(Product product) {
return product.mfgYear <= 2019 && product.expYear <= 2019;
}
static class Product {
String productName;
int mfgYear;
int expYear;
public String getProductName() {
return productName;
}
}
filter() will pass any item for which the lambda expression (or method reference in this case) return true. map() will convert the value passing the item to the method reference and create a stream of whatever type it returns. We pass the name getter in that case.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论