gpt4 book ai didi

java - 在列表中输入几行

转载 作者:行者123 更新时间:2023-11-30 07:45:21 24 4
gpt4 key购买 nike

我有简单的类(class):

public class ForStream {
private int shortId;
private long longId;

public ForStream(int shortId, long longId) {
this.shortId = shortId;
this.longId = longId;
}

public int getShortId() {
return shortId;
}

public void setShortId(int shortId) {
this.shortId = shortId;
}

public long getLongId() {
return longId;
}

public void setLongId(long longId) {
this.longId = longId;
}
}

需要找出数组中几个子串的出现。我这样做(可能无效):

  public static void main(String[] args) {
List<ForStream> resList = List.of(
new ForStream(689, 10000000001L),
new ForStream(781, 10000000001L),
new ForStream(785, 10000000001L),
new ForStream(689, 10000000002L),
new ForStream(689, 10000000003L),
new ForStream(781, 10000000004L),
new ForStream(785, 10000000004L)
);

boolean isEqual = !resList.stream()
.filter(p -> p.getLongId() == 10000000001L)
.filter(p -> p.getShortId() == 689)
.filter(p -> p.getShortId() == 781)
.filter(p -> p.getShortId() == 785)
.collect(Collectors.toList()).isEmpty();

}

返回错误。但 689、781 和 785 包含列表。

最佳答案

这应该有效。您正在过滤掉单个元素。但是您想要做的是将它与值列表进行比较。你正在做的是 && 但你需要的是 ||

   boolean isEqual = !resList.stream()
.filter(p -> p.getLongId() == 10000000001L)
.filter(p -> Arrays.asList(689, 781, 785).contains(p.getShortId()))
.collect(Collectors.toList()).isEmpty();

关于java - 在列表中输入几行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51820733/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com