user1198778
user1198778

Reputation: 389

Why does it keep saying the size of a hashMap is zero after I keep putting things in it?

What I am trying to do is add rooms to a hash map (I don't want duplicates) with addRoom(). I am then passing them to the controller with getRoom(String) or getRooms().

The problem is, as you can see with my multiple System.out.prints, the size keeps staying at 0, no matter how many time I run addRoom().

Am I doing something wrong or is the problem somewhere else in the program?

package calendar;

import java.util.HashMap;
import java.util.Map;

public class Building {

    String buildingName = null;

    private Map<String, Classroom> rooms =
        new HashMap<String, Classroom>();

    public Building() {
    }
    public Building(String name){
        buildingName=name;
    }

    /**
     * @return the rooms
     */
    public Map<String, Classroom> getRooms() {
        System.out.println("room size from building.java:" +
            rooms.size());
        return rooms;
    }

    public Classroom getRoom(String roomName) {
        if (rooms.containsKey(roomName)) {
            return rooms.get(roomName);
        }
        return new Classroom("null");
    }

    /**
     * @param rooms the rooms to set
     */
    public void setRooms(Map<String, Classroom> rooms) {
        this.rooms = rooms;
    }

    public void addRoom(String roomNumber) {
        System.out.println("Room added: "+roomNumber +" to "+ 
            buildingName+ " size:"+rooms.size());
        rooms.put(roomNumber, new Classroom(roomNumber));
    }
}

I call this method from a parser so the code is a bit verbose but I will cut down on it and try to paste just the relevant parts:

private Map<String, Building> currentBuilding = new HashMap<String, Building>();

now this is in a for loop that goes over all the blocks of data that need to be parsed: currentBuilding.put(building, new Building(building)); currentBuilding.get(building).addRoom(room);

Where building and room are Strings. The buildings get initiated as they should. And obviously I want the room of each building to be in its corresponding building but this does not seem to happen as intended.

Upvotes: 0

Views: 1999

Answers (1)

ilya.stmn
ilya.stmn

Reputation: 1624

Your class works perfectly! Just made some fixes

import java.util.HashMap;
import java.util.Map;

public class Building {

 String buildingName = null;
 private static Map<String, Classroom> rooms =
        new HashMap<String, Classroom>();

 public Building() {
 }

 public Building(String name) {
    buildingName = name;
 }

 /**
  * @return the rooms
  */
 public Map<String, Classroom> getRooms() {
    System.out.println("room size from building.java:"
            + rooms.size()); 
    return rooms;
 }

 public Classroom getRoom(String roomName) {
    if (rooms.containsKey(roomName)) {
        return rooms.get(roomName);
    }
    return new Classroom("null");
 }

 /**
  * @param rooms the rooms to set
  */
 public void setRooms(Map<String, Classroom> rooms) {
    this.rooms = rooms; // did you use this method to initialize map?
 }

 public void addRoom(String roomNumber) {
    rooms.put(roomNumber, new Classroom(roomNumber));
    System.out.println("Room added: " + roomNumber + " to "
            + buildingName + " size:" + rooms.size()); // print info after action
 }
}

and usage

    public static void main(String[] args) {
        Building building = new Building("first building");
        building.addRoom("1"); // add room 1
        building.addRoom("2"); // add room 2
        building.addRoom("3"); // add room 3
        System.out.println(building.getRooms().size()); 
    }

ouput

Room added: 1 to first building size:1
Room added: 2 to first building size:2
Room added: 3 to first building size:3
room size from building.java:3
3

Nevertheless, your class works as expected, the problem can be in class usage, how do you handle reference to Building class, may be you want it to be static?

Upvotes: 1

Related Questions