[英]How do I apply an operation using .map lambda expression using a different value from a list each time?
Forgive me for the title im not sure how to articulate this problem as a question. 原谅我的标题,我不确定该如何阐明这个问题。
Anyway, my questions is this.Take this function, 无论如何,我的问题是这个。
function(object o, String a) {
stuff;
return new object o;
}
How would I make it so that when using the .map
lambda expression I could apply this operation to each variable in the stream, in order, so that the I could call the function like this .map(l -> function(l, stringArraylist))
so that the function applies for the string in the equivalent position in its arraylist, as L is in its own stream. 我将如何做到这一点,以便在使用
.map
lambda表达式时,可以将此操作按顺序应用于流中的每个变量,以便我可以像这样调用函数.map(l -> function(l, stringArraylist))
以便该函数在其arraylist中的等效位置应用该字符串,因为L在其自己的流中。
the function applies for the string in the equivalent position in its arraylist, as L is in its own stream
该函数在其arraylist中的等效位置申请字符串,因为L在其自己的流中
This is a bit vague, but it appears you want to apply your function
on pairs of elements belonging to two collections (the first collection is of some unspecified type - let's call it SomeClass
- and the other is an ArrayList<String>
). 这有点含糊,但是似乎您想将
function
应用于属于两个集合的成对元素(第一个集合的类型未指定-我们称它为SomeClass
另一个是ArrayList<String>
)。
It's not entirely clear what function
is supposed to do, since it's not a valid Java method, but based on the return statement I'm assuming it should return an instance of the same type as the argument o
: 目前尚不清楚应该执行什么
function
,因为它不是有效的Java方法,但是基于return语句,我假设它应返回与参数o
相同类型的实例:
SomeClass function(SomeClass o, String a) {
// some logic
return some new instance of SomeClass
}
In order to pair elements of the two collections, you can create an IntStream
of the indices (assuming both collections have the same number of elements). 为了配对两个集合的元素,您可以创建索引的
IntStream
(假设两个集合具有相同数量的元素)。
So, assuming you have a Collection<SomeClass>
and an ArrayList<String>
, you can write: 因此,假设您有
Collection<SomeClass>
和ArrayList<String>
,则可以编写:
Collection<SomeClass> first = ...
ArrayList<String> second = ...
IntStream.range(0,first.size())
.mapToObj (i -> function(first.get(i),second.get(i)))
...
This will result in a Stream<SomeClass>
created by applying function
on each pair of elements. 这将导致通过在每对元素上应用
function
来创建Stream<SomeClass>
。 You can collect these elements into a List<SomeClass>
or do any other required processing. 您可以将这些元素收集到
List<SomeClass>
或进行任何其他必需的处理。
For example: 例如:
List<SomeClass> output =
IntStream.range(0,first.size())
.mapToObj (i -> function(first.get(i),second.get(i)))
.collect(Collectors.toList());
I think you ask about how to use Function<T,R>
in Java 8 in map method of a certain stream. 我想问您有关如何在特定流的map方法中使用Java 8中的
Function<T,R>
。 Here a little example: 这里有个例子:
List<String> list = Arrays.asList("1","2","3","4","5");
// Both functions are equivalent, I write both to clarify the concept using lambda expression.
//Function<? super String, ? extends Integer> function = stringElement -> Integer.parseInt(stringElement);
Function<? super String, ? extends Integer> function = Integer::parseInt;
list.stream().map(function);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.