问题
Originally, I have this code:
String[] A;
String[] B;
//...
List<String> myList= new ArrayList<>(A.length + B.length);
for (int i= 0; i< B.length; i++){
myList.add(A[i]);
myList.add("*".equals(B[i]) ? B[i] : doSomethingWith(B[i]));
}
How to refactor if using, preferably, Java 8?
If for instance I have these arrays
A = {"one", "two", "three", "four"}
B = {"five", "six", "seven", "eight"}
At the end of the code, myList will be:
myList = {"one", "five", "two", "six", "three", "seven", "four", "eight"}
回答1:
I personally don't think this needs refactoring as any "streamed" code will be less readable and less intuitive than your existing code, but as a pure proof-of-concept:
String[] A;
String[] B;
List<String> myList;
myList = IntStream.range(0, B.length)
.mapToObj(i -> new String[]
{
A[i],
"*".equals(B[i]) ? B[i] : doSomethingWith(B[i])
})
.flatMap(Arrays::stream)
.collect(Collectors.toList());
Working demo.
We use
IntStream.rangeto provide the indices into our arrays.mapToObjmaps each index to an array containing the elements we want (this stage is also needed asIntStream::flatMapcan only convert to anotherIntStream, we want to convert it to aStreamofStrings).flatMapmaps each array to a stream, then "flattens" the resulting stream of streams.Finally, we just
collectthe results.
回答2:
Try something like this:
String[] A;
String[] B;
//...
List<String> myList= Arrays.asList (A);
List<String> myList2 = Arrays.stream(B).map(
(x)->"*".equals(x) ? x : doSomethingWith(x))
.collect(Collectors.toList());
myList.addAll(myList2);
来源:https://stackoverflow.com/questions/48225181/how-to-combine-two-lists-without-using-foreach