将多个列表中的每个元素合并为一个元素,并在Java 8中返回另一个列表 [英] Merge each element of multiple lists into one Element and return another list in java 8
本文介绍了将多个列表中的每个元素合并为一个元素,并在Java 8中返回另一个列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为简单起见,我有2个String列表,我需要将这些字符串连接到一个列表中并创建另一个列表. 例如-
for simplicity I have 2 lists of String and I need to join the strings into one and create another list. For eg --
List 1 = [a,b,c,d]
List 2 = [e,f,g,h]
我希望输出为
List3 = [ae,bf,cg,dh]
我可以使用常规的for循环来做到这一点.但是不知道如何进行java8
I can do this using regular for loops. but dont know how to proceed for java8
我正在尝试让自己在n Java 8中思考:-)
I am trying to get myself thinking in n Java 8 :-)
推荐答案
我不确定是否有比通过索引访问两个列表中的元素更好的(简便)方法:
I'm not sure there's a better (easy) way to do this than to access the elements from the two lists by index:
List<String> zipped = IntStream.range(0, list.size())
.mapToObj(i -> list1.get(i) + list2.get(i))
.collect(Collectors.toList());
这篇关于将多个列表中的每个元素合并为一个元素,并在Java 8中返回另一个列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文