给出以下代码:
for (int i = 0; i < noRooms; i++) {
String roomPrice = response.xmlPath().getString("ABC_AvailRS.AccommodationSearchResponse.Accommodations.AccommodationSegment[0].AccommodationUnits.AccommodationUnit[" + i + "].RoomRate.@Amount");
roomPrices.add(roomPrice);
List<Float> floatRoomPrices = roomPrices.stream()
.map(Float::parseFloat)
.collect(Collectors.toList());
float min = floatRoomPrices.stream().min(Float::compare).get();
float max = floatRoomPrices.stream().max(Float::compare).get();
}
...我可以在其中获取金额列表作为字符串金额=&#34; 92.65&#34;,将它们转换为浮点数并添加到列表中,我可以清楚地找到最高和最低金额。
答案 0 :(得分:2)
您可以使用排序列表快速找到此信息。这是一个简单的例子。
public static void main(String[] args) {
List<Float> floats = new ArrayList<>();
floats.add(7.85f);
floats.add(0.85f);
floats.add(9.01f);
floats.add(2.78f);
Collections.sort(floats); // in ascending order, adjust sorting method as preferred - maybe you can utilize stream().sorted
float min = floats.get(0);
float max = floats.get(floats.size() - 1);
int midIndex = floats.size() / 2; // Answer to #2
float midValue = floats.get(midIndex); // Answer to #1
System.out.println("Min: " + min + "\nMax: " + max + "\nMid index: " + midIndex + "\nMid value: " + midValue);
}
这给出了输出:
分钟:0.85
最大:9.01
中期指数:2
中值:7.85