通过 java 8 获取和编辑子列表在主列表中

我有很多主列表,其中有一个子列表。

如何访问子列表并通过java 8进行编辑。


例子。


List<Student> list = ....; 

list[0]  = {id = 1, name = "aa", ListMinute = List<String> ls1}

list[1]  = {id = 2, name = "bb", ListMinute = List<String> ls2}

list[2]  = {id = 3, name = "cc", ListMinute = List<String> ls3}

list[3]  = {id = 4, name = "dd", ListMinute = List<String> ls4}

list[0] 的子列表示例:


List<String> ls1 = {"120", "150", "45", "195"}; //List in minutes.

如何将所有子列表从分钟转换为小时和分钟。

list[0] 的子列表的输出:


List<String> ls1 = {"2", "2.30", "0.45", "3.15"}; // List in hours and minutes.


慕的地8271018
浏览 159回答 2
2回答

呼啦一阵风

获得转换的方法之一可能是:List<String> ls1 = List.of("120", "150", "45", "195"); //List in minutes.List<String> out = ls1.stream()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.mapToInt(Integer::parseInt)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.mapToObj(min -> String.format("%d.%d", min / 60, min % 60))&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.collect(Collectors.toList());// would output 2.0 instead of 2 though如果你想更新你现有的List,你可以试试replaceAllList<String> ls1 = Stream.of("120", "150", "45", "195").collect(Collectors.toList()); //List in minutes.ls1.replaceAll(a -> {&nbsp; &nbsp; int min = Integer.parseInt(a);&nbsp; &nbsp; return String.format("%d.%d", min / 60, min % 60);});

青春有我

您可以像这样修改您的list实例replaceAlllist.stream()&nbsp; &nbsp; &nbsp; &nbsp; .map(Student::getListMinute)&nbsp; &nbsp; &nbsp; &nbsp; .forEach(minutes ->&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; minutes.replaceAll(min -> format("%d.%d", parseInt(min) / 60, parseInt(min) % 60))&nbsp; &nbsp; &nbsp; &nbsp; );
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java