0

I need to create a Hallway class which will have inside 2 ArrayLists of Stand objects , one for the stands on the right, and the other for the ones on the left.

My intention is to put these ArrayLists inside another collection in this class.

I don't know if I should use a Hashtable, a Map, etc.

More importantly, my intention is to then access these ArrayLists using a method like:

TheHashTable["Right"].add(standObject); // Add a Stand to the Right Stands ArrayList which is inside a Hashtable.

Example:

   public class Hallway {       

      private Hashtable< String, ArrayList<<Stand> > stands;

      Hallway(){
         // Create 2 ArrayList<Stand>)
         this.stands.put("Right", RightStands);
         this.stands.put("Left", LeftStands);
      }      

      public void addStand(Stand s){
         this.stands["Right"].add(s);
      }
}

Would this be possible?

5 Answers 5

3

It is possible, but I would advise against it. If you have only two stand locations, it would be much and clearer to simply have two variables of type List<Stand>: leftStands and rightStands, and to have corresponding methods: addLeftStand(Stand), addRightStand(Stand), etc. The code would be much clearer, simpler and safer.

If you really want to go your way, the keys of the map shouldn't be Strings. The caller wouldn't know which key to pass to your methods (there are an infinity of Strings), and ven if he knows that the keys are "Right" and "Left", he could make a typo which would go unnoticed by the compiler. You should use an enum instead, which would make the code self-documented and safer:

public enum Location {
    LEFT, RIGHT
}

private Map<Location, List<Stand>> stands = new HashMap<Location, List<Stand>>();

public Hallway() {
    for (Location location : Location.values()) {
        stands.put(location, new ArrayList<Stand>());
    }
}

public void addStand(Location location, Stand stand) {
    stands.get(location).add(stand);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Could I suggest for (Location l : Location.values()) { stands.put(l, new ArrayList<Stand>()); } in the constructor? In this case, if more locations are added, they will be initialised.
@msandiford: agreed. I'll change my answer.
2

if you only have right and left, you could for example just create 2 array lists.

private ArrayList<Stand> rightStands;
private ArrayList<Stand> leftStands;

2 Comments

yes, I thought about that. But that way i can't access the ArrayLists like > this.stands["Right"].add(s);
that syntax is not correct, and why do you want to access it like that?
1

If I understood your question clearly, then this is what you want:

public void addStand(Stand s){
 this.stand.get("Right").add(s);
}

But a better approach would be to use Map instead of Hashtable.

public class Hallway {

 private Map< String, ArrayList<<Stand> > stands;
 private List<Stand> RightStands;
 private List<Stand> LeftStands;

 Hallway(){
    stands = new HashMap();
    RightStands = new ArrayList();
    LeftStands = new ArrayList();
    this.stands.put("Right", RightStands);
    this.stands.put("Left", LeftStands);
  }      

  public void addStand(Stand s){
     this.stands.get("Right").add(s);
  }
}

Comments

0

You need a multi-map, for example from Commons Collections or Guava.

Those will let you map multiple values (Stand1, Stand2, ...) to a single key (e.g. "right").

For example (with Commons Collections):

MultiMap stands = new MultiHashMap();
stands.put("left", new Stand());
stands.put("left", new Stand());
stands.put("right", new Stand());
stands.put("right", new Stand());
stands.put("right", new Stand());
Collection standsOnLeftSide = (Collection) stands.get("left");

I think though that Guava is preferrable because it supports Generics.

Comments

0

Don't use HashTable. It has been deprecated long back. Use TreeMap or HashMap.

List<Stand> right=new ArrayList<Stand>(),left=new ArrayList<Stand>();
Map<String,List<Stand> > stands= new HashMap<String, List<Stand> >();
stands.put("right",right);
stands.put("left",left);

To learn about maps and to decide which Map suits you best read Precisely Concise: Java Maps

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.