2 回答
TA贡献1802条经验 获得超6个赞
获得转换的方法之一可能是:
List<String> ls1 = List.of("120", "150", "45", "195"); //List in minutes.
List<String> out = ls1.stream()
.mapToInt(Integer::parseInt)
.mapToObj(min -> String.format("%d.%d", min / 60, min % 60))
.collect(Collectors.toList());
// would output 2.0 instead of 2 though
如果你想更新你现有的List,你可以试试replaceAll
List<String> ls1 = Stream.of("120", "150", "45", "195").collect(Collectors.toList()); //List in minutes.
ls1.replaceAll(a -> {
int min = Integer.parseInt(a);
return String.format("%d.%d", min / 60, min % 60);
});
TA贡献1784条经验 获得超8个赞
您可以像这样修改您的list实例replaceAll
list.stream()
.map(Student::getListMinute)
.forEach(minutes ->
minutes.replaceAll(min -> format("%d.%d", parseInt(min) / 60, parseInt(min) % 60))
);
添加回答
举报