Using Java 8 streams with lambdas to process a for loop, invoking a method with multiple parms

81 views Asked by At

I have a for loop, processing two string lists, which invokes a method with multiple parms, returning an object, which gets added to a List

I want to effectively utilize stream/lambda for this, can someone guide me I have two incoming string lists "AAA, BBB, CCC" and a corresponding list of quantities as "1, 3, 11"

final List<someObj> someObjs = new ArrayList<someObj>() ;

final List<String> codesList = Arrays.asList(codes.split("\\s*,\\s*"));
final List<String> qtysList  = Arrays.asList(qtys.split("\\s*,\\s*"));

for (String code: codesList){
    someObjs.add(addThis(code, qtysList.get(index++)));//
}

return someObj;

How can I convert this using lambdas ? Thanks in advance !

1

There are 1 answers

3
Ravindra Ranwala On BEST ANSWER

How about this,

final List<SomeObj> someObjs = IntStream.range(0, codesList.size())
        .mapToObj(i -> addThis(codesList.get(i), qtysList.get(i)))
        .collect(Collectors.toList());