Bernardo Wilberger
Bernardo Wilberger

Reputation: 85

Java Collections

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?

Upvotes: 0

Views: 2395

Answers (5)

Sai Sunder
Sai Sunder

Reputation: 1021

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

Upvotes: 0

JB Nizet
JB Nizet

Reputation: 691625

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);
}

Upvotes: 3

Jay
Jay

Reputation: 37

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.

Upvotes: 0

AllTooSir
AllTooSir

Reputation: 49352

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);
  }
}

Upvotes: 1

Vegard
Vegard

Reputation: 1922

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

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

Upvotes: 2

Related Questions