Reputation: 3
good afternoon, I would need help with the following method where I have a list of rooms, for example, and it is to print a URL with the data. only that the last data of the list would need to remove the .append(";") which would be the separator and replace it with (" ") or remove the ; . Only in the cases of the last object (room),
I investigated but I would not know what is best if the size or the lastIndexOf
example
rooms.get(rooms.size()-1); -->option 1
rooms.lastIndexOf(room); --> option 2
the url comes as follows, https://hotel.google.net/hotels/ytd.proba.miami,2/MIA/XXXXX-XXXXX/1DR;1DR;?token=Ws2QmzjsxDFC7jeN&
I would only need the last 1DR to not have the ; just the last. If I'm missing any more information, tell me. Thanks a lot
private String getFormattedRooms(List<HotelRoom> rooms) {
StringBuilder formattedRooms = new StringBuilder();
for (HotelRoom room : rooms) {
formattedRooms.append("1").append(getSupplierRoomType(room)).append(getChildrenAges(room)).append(";");
}
return formattedRooms.toString();
}
Upvotes: 0
Views: 38
Reputation: 27149
What you're actually building is a series of strings joined by ';'
, so why not build each string and then join them?
With streams, for example:
rooms.stream()
.map(room -> "1" + getSupplierRoomType(room) + getChildrenAges(room))
.collect(Collectors.joining(";"));
Without streams:
private String getFormattedRooms(List<HotelRoom> rooms) {
List<String> roomStrings = new ArrayList<>();
for (HotelRoom room : rooms) {
roomStrings.add("1" + getSupplierRoomType(room) + getChildrenAges(room));
}
return String.join(";", roomStrings);
}
Upvotes: 4