Java 8 Stream,按条件添加一个元素多次列出;
Java 8 Stream, add one element to list many time by condition;
我有这样的行↓从列表中获取确切的元素,但我想使用某种数组多次添加它,比如 "for" 和计数器
list.stream().filter(x -> x.getUserID() == user.getUserID()).collect(Collectors.toList());
list.stream().map(o -> new Object[] { (Object) o }).collect(Collectors.toList();
我有类似的代码,但我不想使用 double 用于:
List<Object[]> tmp = new ArrayList<Object[]>();
for (Iterator<?> iterator = tests.getTestData().iterator(); iterator.hasNext();) {
Object objects = iterator.next();
//should have condition like id=id
for (int i = 0; i < t.getInvocationCount(it); i++) {
tmp.add(new Object[] { objects });
}
}
多个满足条件的元素可以使用stream吗?
编辑:
*tests.getTestData() -> returns List
**t.getInvocationCount -> returns int [t is not important cause it is generic]
我只需要多个元素入库,注意
FOR arry TO arry=END DO:
IF arry[i] IS statment=true DO:
FOR 0 TO outsideCounter_i DO:
tempArry.add(arry[i])
其中 * 是 arry
,** 是 outsideCounter
如果声明为真,我想要多个元素使用流。
如果仍然不清楚,请添加评论。
我读到了 nCopies,它是 "cool" 但我可以在流中使用它吗?
您可以使用 IntStream 作为用于复制元素的索引。
这样的事情应该可行(我不完全确定你的两个代码片段是如何相关的,所以我可能把名字弄错了):
List<Object[]> tmp =
tests.getTestData().stream()
.filter(x -> x.getUserID() == user.getUserID()) // not sure about this
// part, since it's not
// clear if the elements
// of the input
// Iterable have a
// getUserID method
.flatMap (x -> IntStream.range(0,t.getInvocationCount(it)).mapToObj(i -> x))
.map(o -> new Object[] {o})
.collect (Collectors.toList());
正如 aioobe 评论的那样,Collections.nCopies
方法在这里很有用:
List<Object[]> tmp =
tests.getTestData().stream()
.filter(x -> x.getUserID() == user.getUserID()) // not sure about this
// part, since it's not
// clear if the elements
// of the input
// Iterable have a
// getUserID method
.flatMap (o -> Collections.nCopies(t.getInvocationCount(it),o).stream())
.map (o -> new Object[] {o})
.collect (Collectors.toList());
我有这样的行↓从列表中获取确切的元素,但我想使用某种数组多次添加它,比如 "for" 和计数器
list.stream().filter(x -> x.getUserID() == user.getUserID()).collect(Collectors.toList());
list.stream().map(o -> new Object[] { (Object) o }).collect(Collectors.toList();
我有类似的代码,但我不想使用 double 用于:
List<Object[]> tmp = new ArrayList<Object[]>();
for (Iterator<?> iterator = tests.getTestData().iterator(); iterator.hasNext();) {
Object objects = iterator.next();
//should have condition like id=id
for (int i = 0; i < t.getInvocationCount(it); i++) {
tmp.add(new Object[] { objects });
}
}
多个满足条件的元素可以使用stream吗?
编辑:
*tests.getTestData() -> returns List
**t.getInvocationCount -> returns int [t is not important cause it is generic]
我只需要多个元素入库,注意
FOR arry TO arry=END DO:
IF arry[i] IS statment=true DO:
FOR 0 TO outsideCounter_i DO:
tempArry.add(arry[i])
其中 * 是 arry
,** 是 outsideCounter
如果声明为真,我想要多个元素使用流。 如果仍然不清楚,请添加评论。
我读到了 nCopies,它是 "cool" 但我可以在流中使用它吗?
您可以使用 IntStream 作为用于复制元素的索引。
这样的事情应该可行(我不完全确定你的两个代码片段是如何相关的,所以我可能把名字弄错了):
List<Object[]> tmp =
tests.getTestData().stream()
.filter(x -> x.getUserID() == user.getUserID()) // not sure about this
// part, since it's not
// clear if the elements
// of the input
// Iterable have a
// getUserID method
.flatMap (x -> IntStream.range(0,t.getInvocationCount(it)).mapToObj(i -> x))
.map(o -> new Object[] {o})
.collect (Collectors.toList());
正如 aioobe 评论的那样,Collections.nCopies
方法在这里很有用:
List<Object[]> tmp =
tests.getTestData().stream()
.filter(x -> x.getUserID() == user.getUserID()) // not sure about this
// part, since it's not
// clear if the elements
// of the input
// Iterable have a
// getUserID method
.flatMap (o -> Collections.nCopies(t.getInvocationCount(it),o).stream())
.map (o -> new Object[] {o})
.collect (Collectors.toList());