使用Java 8比较给定列表中的值。

huangapple go评论74阅读模式
英文:

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&lt;Product&gt; productList = getProductList();

I have list of products here.

Have to iterate each one of the Product from the list and get the List&lt;String&gt; productName that lies in the range between mfgYear & expYear for a given 2019(testYear).

For example, 
mfgYear &lt;= 2019 &lt;= 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&lt;String&gt; productNames = 
                  products.stream()
                          .filter(p -&gt; p.mfgYear &lt;= givenYear &amp;&amp; givenYear &lt;= 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 &lt;= testYear &amp;&amp; testYear &lt;= expYear:
}

List&lt;String&gt; productNames = products.stream()
                                    .filter(p -&gt; 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&lt;String&gt; process(List&lt;Product&gt; productList) {
    return productList.stream()
            .filter(this::isWithinRange)
            .map(Product::getProductName)
            .collect(Collectors.toList());
}

boolean isWithinRange(Product product) {
    return product.mfgYear &lt;= 2019 &amp;&amp; product.expYear &lt;= 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.

huangapple
  • 本文由 发表于 2020年9月15日 11:20:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/63894578.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定