Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/401.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何从流方法返回特定属性_Java_Oop_Java Stream - Fatal编程技术网

Java 如何从流方法返回特定属性

Java 如何从流方法返回特定属性,java,oop,java-stream,Java,Oop,Java Stream,我在我的item类中创建了一个方法,该方法可以过滤少于2个数量的数组列表项 当我只需要它的名称和价格时,该方法返回该项目的所有属性 我被困在我的方法需要改变的地方 public static List<item> findquantity(List<item> items) { return items.stream() .filter(item -> item.getquantity()< 2) .collect(Coll

我在我的item类中创建了一个方法,该方法可以过滤少于2个数量的数组列表项


当我只需要它的名称和价格时,该方法返回该项目的所有属性

我被困在我的方法需要改变的地方

public static List<item>  findquantity(List<item> items) { 
    return items.stream()
      .filter(item -> item.getquantity()< 2)
      .collect(Collectors.toList());
}

如果将
findQuantity
的返回类型改为
Map
而不是
List
,则可以使用
Collectors.toMap()
仅返回
name
price

public static Map<String, Double>  findQuantity(List<Item> items) { 
    return items.stream().filter(item -> item.getQuantity() < 2)
            .collect(Collectors.toMap(Item::getName, Item::getPrice));
}
公共静态映射findQuantity(列表项){
return items.stream().filter(item->item.getQuantity()<2)
.collect(Collectors.toMap(Item::getName,Item::getPrice));
}
更好的方法是创建一个自定义对象,该对象保存@Thomas在注释中引用的值。应该是这样的:

items.stream().filter(item -> item.getQuantity() < 2).map(i -> new CustomItem(i.getName(), i.getPrice()))
                .collect(Collectors.toList());
items.stream().filter(item->item.getQuantity()<2).map(i->newcustomitem(i.getName(),i.getPrice())
.collect(Collectors.toList());
您可以尝试以下方法: 首先,您必须创建一个包含两个字段(price和name)的类,例如:

   Class Price{
    String name;
    BigDecimal price;// I don't Know is your quantity is int, Double or Float
    //Any other property that you need
    
    //Constructor
    //Getter & Setter
    }
修改你的方法:

   public static List<Price>  findquantity(List<item> items) {

List<Price> lstPrice = new ArrayList<>();
items.stream().filter(it -> it.getquantity()< 2).map((vals) -> {
            Price item = new Price();
            item.setName(vals.getName());
            item.setPrice(vals.getquantity()); 
            //set any other property that you need           
            return item;
        }).forEachOrdered((item) -> {
            lstPrice.add(item);
        });
 return lstPrice;
}
公共静态列表findquantity(列表项){
List lstPrice=new ArrayList();
items.stream().filter(it->it.getquantity()<2).map((VAL)->{
价格项=新价格();
item.setName(vals.getName());
item.setPrice(vals.getquantity());
//设置所需的任何其他属性
退货项目;
}).forEachOrdered((项目)->{
增加价格(项目);
});
退货价格;
}

仅此而已,我希望这对您有所帮助

“当我只想要它的名称和价格时,该方法返回该项目的所有属性。”该方法返回一个项目列表,也许您希望它返回其他内容,您所说的“我只想要它的名称和价格”是什么意思,它是两个字符串的数组吗?一对物体?一些自定义对象?过滤后,您需要一个
.map(item->objectOnlyContainingNameAndPrice)
,即创建一个只包含name和price的类,并使用item值创建实例。另一方面,请遵守Java命名约定,即您的类应命名为
item
。否则,类名可能会与实例名
item
混淆。我的方法返回过滤后的少于2个数量的项目及其所有属性,如项目名称、项目价格、项目数量、项目id。我希望它只返回名称和价格任何自定义对象看起来都是
记录的良好用例。
   public static List<Price>  findquantity(List<item> items) {

List<Price> lstPrice = new ArrayList<>();
items.stream().filter(it -> it.getquantity()< 2).map((vals) -> {
            Price item = new Price();
            item.setName(vals.getName());
            item.setPrice(vals.getquantity()); 
            //set any other property that you need           
            return item;
        }).forEachOrdered((item) -> {
            lstPrice.add(item);
        });
 return lstPrice;
}