使用 IntStream 而不是流迭代两个列表列表
Iterate over two Lists of Lists with IntStream instead of streams
我正在尝试使用流来迭代两个列表列表,以验证同一索引的内部列表大小是否相同。我已经设法使用流实现了这一点,但我必须使用 IntStream
和 mapToObj
.
重写
我目前的做法是:
List<List<String>> a = config.getStrips();
List<List<Integer>> b = slotMachineConfig.getWeights();
a.stream()
.filter(first ->
b.stream()
.allMatch(second -> second.size() == first.size())
)
.findFirst()
.orElseThrow(InvalidConfigException::new);
问题是我不能确定大列表的大小是否对应,所以我必须使用 IntStream 重写它,并为每个列表使用索引。
我目前拥有的,但看起来像这样不起作用,我正在尝试编写一个“验证”函数来验证内部列表,但似乎我在那里收到一条错误消息,说“没有实例类型变量 U 的存在使得 void 符合 U".
IntStream.range(0, a.size())
.mapToObj(i -> validate(i, a.get(i), b.get(i)))
.findFirst()
.orElseThrow(SlotMachineInvalidConfigException::new);
public void validate(int index, List<String> firstList, List<Integer> secondList) {
如何使用 IntStream
和 mapToObj
重写我的方法,有人可以帮助我吗?
如果我理解正确的话,我认为这样的事情会起作用:
List<List<String>> a = config.getStrips();
List<List<Integer>> b = slotMachineConfig.getWeights();
if (a.size() != b.size()) throw new InvalidConfigException();
boolean allTheSame = IntStream.range(0, a.size())
.map(i -> a.get(i).size() - b.get(i).size())
.allMatch(diff -> diff == 0);
if (!allTheSame) throw new InvalidConfigException();
您的想法是正确的,但如果您只是比较尺寸,则并不需要单独的验证函数。这是一个支持任何列表类型的工作示例:
public class ListSizeMatcher {
public <T,S> boolean sizeMatches(List<List<T>> list1, List<List<S>> list2) {
return list1.size() == list2.size()
&& IntStream.range(0, list1.size())
.allMatch(i -> list1.get(i).size() == list2.get(i).size());
}
public static void main(String[] args) {
ListSizeMatcher matcher = new ListSizeMatcher();
System.out.println(matcher.sizeMatches(List.of(List.of(1)), List.of(List.of("a"), List.of("b"))));
System.out.println(matcher.sizeMatches(List.of(List.of(1)), List.of(List.of("a", "b"))));
System.out.println(matcher.sizeMatches(List.of(List.of(1, 2)), List.of(List.of("a", "b"))));
}
}
请注意,从设计的角度来看,如果列表中的每个项目都与单独列表中的相应项目相匹配,您最好创建一个包含这两个项目的单个 class。
郑重声明,您的验证函数 returns void
但我假设它是 return 布尔值
这里有一个更紧凑的版本
List<List<String>> a = new LinkedList<>();
List<List<Integer>> b = new LinkedList<>();
boolean match = IntStream.range(0, a.size())
.mapToObj(i -> a.get(i).size() == b.get(i).size())
.reduce(Boolean::logicalAnd).orElseThrow(InvalidConfigException::new);
if (!match) {
throw new InvalidConfigException();
}
选择:
List<List<String>> a = new LinkedList<>();
List<List<Integer>> b = new LinkedList<>();
if (IntStream.range(0, a.size()).filter(i -> a.get(i).size() != b.get(i).size()).count() > 0){
throw new InvalidConfigException();
};
一天结束时,只需要 1 个不同就失败了。
错误意味着 validate
方法不能为 void,它应该是 return 一些有效值(可能是布尔值)。
如果内部列表应该具有相同的大小才有效,则检查可能如下所示:
// assuming the sizes of outer lists are equal
boolean allSizesEqual = IntStream.range(0, a.size())
.allMatch(i -> a.get(i).size() == b.get(i).size());
if (!allSizesEqual) {
throw new InvalidConfigException("Not all sizes are valid");
}
如果需要查找检测到差异的特定索引:
List<Integer> badIndexes = IntStream.range(0, a.size())
.filter(i -> a.get(i).size() != b.get(i).size()) // IntStream
.boxed() // Stream<Integer>
.collect(Collectors.toList());
if (!badIndexes.isEmpty()) {
throw new InvalidConfigException("Different indexes found: " + badIndexes);
}
或者 validate
方法可以固定为 return 过滤器的适当值:
boolean allItemsValid = IntStream.range(0, a.size())
.allMatch(i -> listsAreValid(a.get(i), b.get(i)));
if (!allItemsValid) {
throw new InvalidConfigException("Not all entries are valid");
}
public boolean listsAreValid(List<String> innerA, List<Integer> innerB) {
// any advanced logic
return innerA.size() == innerB.size();
}
我正在尝试使用流来迭代两个列表列表,以验证同一索引的内部列表大小是否相同。我已经设法使用流实现了这一点,但我必须使用 IntStream
和 mapToObj
.
我目前的做法是:
List<List<String>> a = config.getStrips();
List<List<Integer>> b = slotMachineConfig.getWeights();
a.stream()
.filter(first ->
b.stream()
.allMatch(second -> second.size() == first.size())
)
.findFirst()
.orElseThrow(InvalidConfigException::new);
问题是我不能确定大列表的大小是否对应,所以我必须使用 IntStream 重写它,并为每个列表使用索引。
我目前拥有的,但看起来像这样不起作用,我正在尝试编写一个“验证”函数来验证内部列表,但似乎我在那里收到一条错误消息,说“没有实例类型变量 U 的存在使得 void 符合 U".
IntStream.range(0, a.size())
.mapToObj(i -> validate(i, a.get(i), b.get(i)))
.findFirst()
.orElseThrow(SlotMachineInvalidConfigException::new);
public void validate(int index, List<String> firstList, List<Integer> secondList) {
如何使用 IntStream
和 mapToObj
重写我的方法,有人可以帮助我吗?
如果我理解正确的话,我认为这样的事情会起作用:
List<List<String>> a = config.getStrips();
List<List<Integer>> b = slotMachineConfig.getWeights();
if (a.size() != b.size()) throw new InvalidConfigException();
boolean allTheSame = IntStream.range(0, a.size())
.map(i -> a.get(i).size() - b.get(i).size())
.allMatch(diff -> diff == 0);
if (!allTheSame) throw new InvalidConfigException();
您的想法是正确的,但如果您只是比较尺寸,则并不需要单独的验证函数。这是一个支持任何列表类型的工作示例:
public class ListSizeMatcher {
public <T,S> boolean sizeMatches(List<List<T>> list1, List<List<S>> list2) {
return list1.size() == list2.size()
&& IntStream.range(0, list1.size())
.allMatch(i -> list1.get(i).size() == list2.get(i).size());
}
public static void main(String[] args) {
ListSizeMatcher matcher = new ListSizeMatcher();
System.out.println(matcher.sizeMatches(List.of(List.of(1)), List.of(List.of("a"), List.of("b"))));
System.out.println(matcher.sizeMatches(List.of(List.of(1)), List.of(List.of("a", "b"))));
System.out.println(matcher.sizeMatches(List.of(List.of(1, 2)), List.of(List.of("a", "b"))));
}
}
请注意,从设计的角度来看,如果列表中的每个项目都与单独列表中的相应项目相匹配,您最好创建一个包含这两个项目的单个 class。
郑重声明,您的验证函数 returns void
但我假设它是 return 布尔值
这里有一个更紧凑的版本
List<List<String>> a = new LinkedList<>();
List<List<Integer>> b = new LinkedList<>();
boolean match = IntStream.range(0, a.size())
.mapToObj(i -> a.get(i).size() == b.get(i).size())
.reduce(Boolean::logicalAnd).orElseThrow(InvalidConfigException::new);
if (!match) {
throw new InvalidConfigException();
}
选择:
List<List<String>> a = new LinkedList<>();
List<List<Integer>> b = new LinkedList<>();
if (IntStream.range(0, a.size()).filter(i -> a.get(i).size() != b.get(i).size()).count() > 0){
throw new InvalidConfigException();
};
一天结束时,只需要 1 个不同就失败了。
错误意味着 validate
方法不能为 void,它应该是 return 一些有效值(可能是布尔值)。
如果内部列表应该具有相同的大小才有效,则检查可能如下所示:
// assuming the sizes of outer lists are equal
boolean allSizesEqual = IntStream.range(0, a.size())
.allMatch(i -> a.get(i).size() == b.get(i).size());
if (!allSizesEqual) {
throw new InvalidConfigException("Not all sizes are valid");
}
如果需要查找检测到差异的特定索引:
List<Integer> badIndexes = IntStream.range(0, a.size())
.filter(i -> a.get(i).size() != b.get(i).size()) // IntStream
.boxed() // Stream<Integer>
.collect(Collectors.toList());
if (!badIndexes.isEmpty()) {
throw new InvalidConfigException("Different indexes found: " + badIndexes);
}
或者 validate
方法可以固定为 return 过滤器的适当值:
boolean allItemsValid = IntStream.range(0, a.size())
.allMatch(i -> listsAreValid(a.get(i), b.get(i)));
if (!allItemsValid) {
throw new InvalidConfigException("Not all entries are valid");
}
public boolean listsAreValid(List<String> innerA, List<Integer> innerB) {
// any advanced logic
return innerA.size() == innerB.size();
}