使用 Java 流将 Java 列表转换为另一个列表

我有一个班级测试


public class Test{

  String codes;

  String field 1;

  ....

  String field n;

}

我有一个测试对象列表


List<Test> objects, code can be one or more with a comma separated

testObj1("A", "field1".."fieldn")

testObj2("B,C", ...)

testObj3("D,E,F", ....)

testObj4("G", ...)

尝试通过保留剩余字段将其转换list1为新的,每个代码 A、B、C... 到它自己的对象。list2


List<Test>

testObj1("A", ....)

testObj2("B", ....)

testObj3("C", ....)


list1.stream().collect(Collectors.toList())

我使用循环(Sudo 代码)实现了这一点,但正在寻找更好的逻辑


for(loop thru list1){

  String[] codesArr = testObj1.codes.split(",");

  for (String code : codesArr) {

    //Create new Obj 

    Test obj = new Test(code, testObj1.copyotherfields);

    //Add obj to list2

  }

}


守着一只汪
浏览 279回答 3
3回答

素胚勾勒不出你

您可以使用Stream.mapas flatMap:List<Test> finalList = list1.stream()&nbsp; &nbsp; &nbsp; &nbsp; .flatMap(e -> Arrays.stream(e.getCodes().split(","))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .map(c -> new Test(c, e.getField1(), e.getFieldn())))&nbsp; &nbsp; &nbsp; &nbsp; .collect(Collectors.toList());这假定您的Test类将具有类似于以下实现的构造函数:class Test {&nbsp; &nbsp; String codes;&nbsp; &nbsp; String field1;&nbsp; &nbsp; String fieldn;&nbsp; &nbsp; // would vary with the number of 'field's&nbsp; &nbsp; Test(String codes, String field1, String fieldn) {&nbsp; &nbsp; &nbsp; &nbsp; this.codes = codes;&nbsp; &nbsp; &nbsp; &nbsp; this.field1 = field1;&nbsp; &nbsp; &nbsp; &nbsp; this.fieldn = fieldn;&nbsp; &nbsp; }&nbsp; &nbsp; // getters and setters}

达令说

您可以将其简化为:List<Test>&nbsp;copy&nbsp;=&nbsp;list.stream() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.map(e&nbsp;->&nbsp;Arrays.stream(e.codes.split(""))&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.map(c&nbsp;->&nbsp;new&nbsp;Test(c,&nbsp;e.otherField)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.collect(Collectors.toList())) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.findAny().orElse(...);它将流过给定的列表,然后流过Arrayyield fromsplit()并映射到一个新Test对象并将其收集到一个List.&nbsp;它通过 检索它findAny(),它返回一个Optional<List<Test>>,所以我建议使用orElse它来检索默认值。

慕哥9229398

您可以使用一个map函数,然后flatMap它就像这样:List<String> testList = Arrays.asList("one,two,three,four", "five", "six", "seven",&nbsp;"eight, nine", "ten");&nbsp;List<String> reMappedList = testList.stream()&nbsp;.map(s -> {&nbsp; &nbsp; &nbsp;String[] array = s.split(",");&nbsp; &nbsp; &nbsp;return Arrays.asList(array);&nbsp;})&nbsp;.flatMap(List::stream)&nbsp;.collect(Collectors.toList());&nbsp;System.out.println(reMappedList);
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java