简体   繁体   English

如何使用使用.map lambda表达式的操作每次使用与列表不同的值?

[英]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.

相关问题 通过每次使用不同的 BigDecimal 属性改进 Map/Reduce lambda 表达式 - Improve Map/Reduce lambda expression by using a different BigDecimal attribute each time 如何在列表中循环并应用不同键元素的算术运算并使用 Java 8 和递归计算最终值 - How to loop in the list and apply an Arithmetic operation of different keys elements and calculate the final value using Java 8 and Recursion 使用Lambda表达式将列表转换为地图不起作用 - Converting List to Map using Lambda Expression Not working 如何从 map 获取键和值,并使用 Java8 方法对每个键和值执行某些操作 - How to get the key and value from map and perform certain operation on each key and value using Java8 methods 如何在Java中使用Lambda表达式添加地图 - How to add Map using lambda expression in java 如何遍历具有价值的地图Map <String,Integer> 声明为地图 <String,Map<String,Integer> &gt;使用表达语言? - How do I iterate through a map that has value, Map<String,Integer> declared as Map<String,Map<String,Integer>>using Expression Language ? 使用 lambda 表达式使用可选键访问 map 中的值 - Using lambda expression to access a value in a map using an optional key 使用Lambda表达式从地图创建SortedMap的地图 - Create a Map of SortedMap from a Map using lambda expression 使用来自不同类层次结构路由的两个字段进行map-filter lambda表达式 - Using two fields from different class hierarchy routes for map-filter lambda expression 在另一个Lambda表达式中使用变量值 - Using variable value in a lambda expression from another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM