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))
);
添加回答
舉報
