My List contains House objects with the following fields:
private Integer address;
private String street;
private double price;
private int rooms;
I would like to generate a report that lists the number rooms, how many house have that quantity of rooms, and then a listing of those houses. Similar to below.
houses with 2 rooms: 3
-Mainstreet, 1112, $45,000.00
-Mainstreet, 1456, $42,200.00
-Oak, 54, 43,600.00
houses with 3 rooms: 1
-Mainstreet, 1890, $52,000.00
I was thinking of a hashmap with room/quantity key pairs and then using the hashmap to build an array, but there has to be something easier than running a bunch of loops. Any suggestions?
This is what you need:
Then iterate once over over your list to build the map.
map.get(3) will return list houses having 3 rooms;
Then you are done.