我试图Deals
根据我的Deal
选项过滤我,但当我尝试过滤代码时,我得到一个Set<DealOptions>
不能转换为布尔值的类型不匹配错误.当所有交易选项都是红色时,我想保留交易.
这是我的代码:
import java.util.*;
import java.util.stream.Collectors;
class Deal {
String dealname;
String dealprice;
Set<DealOptions> dealop;
public String getDealname() {
return dealname;
}
public void setDealname(String dealname) {
this.dealname = dealname;
}
public String getDealprice() {
return dealprice;
}
public void setDealprice(String dealprice) {
this.dealprice = dealprice;
}
public Set<DealOptions> getDealop() {
return dealop;
}
public void setDealop(Set<DealOptions> dealop) {
this.dealop = dealop;
}
}
class DealOptions {
String optname;
String color;
public String getOptname() {
return optname;
}
public void setOptname(String optname) {
this.optname = optname;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
}
public class Test {
public static void main(String[] args)
{
Deal s = new Deal();
Set<DealOptions> ops = new HashSet<DealOptions>();
DealOptions op = new DealOptions();
s.setDealname("mongo");
s.setDealprice("500");
op = new DealOptions();
op.setColor("red");
op.setOptname("redop");
ops.add(op);
op = new DealOptions();
op.setColor("blue");
op.setOptname("blueop");
ops.add(op);
op = new DealOptions();
op.setColor("green");
op.setOptname("greenop");
ops.add(op);
s.setDealop(ops);
List<Deal> dl = new ArrayList<Deal>();
dl.add(s);
ops = new HashSet<DealOptions>();
s = new Deal();
op = new DealOptions();
s.setDealname("test2");
s.setDealprice("200");
op = new DealOptions();
op.setColor("indigo");
op.setOptname("indigop");
ops.add(op);
op = new DealOptions();
op.setColor("violet");
op.setOptname("violetop");
ops.add(op);
op = new DealOptions();
op.setColor("orange");
op.setOptname("orangeop");
ops.add(op);
s.setDealop(ops);
dl.add(s);
List<Deal> dev = dl.stream().filter(
(p) -> p.getDealop().stream().filter((po) -> po.getColor().equals("red")).collect(Collectors.toSet()))
.collect(Collectors.toList()); // error here
}
}
Run Code Online (Sandbox Code Playgroud)
错误:
无法从Set转换为boolean
如何纠正此错误如何根据我的交易选项过滤我的交易?
您可以allMatch(predicate)
用来确定期权的所有交易是否为红色:
返回此流的所有元素是否与提供的谓词匹配.
在这种情况下,谓词只是告诉选项是否为红色.
List<Deal> output =
dl.stream()
.filter(d -> d.getDealop().stream().allMatch(po -> po.getColor().equals("red")))
.collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
4623 次 |
最近记录: |