Jack
Jack

Reputation: 351

Is it possible to have a hashmap with 4 objects?

Can I have an hashMap with say ID as my key and info, name, quantity as my values?

ok, say I have a class (Products) already that sets my variables, getters and setters. In my Invoice class, which is where the hashMap would be. Would I put like:

private HashMap<String, Products> keys = new HashMap<String, Products>

I'm not quite sure how to access the HashMap though. Say I implement a class that allows me to add and remove invoices from the HashMap, I do not know what the values would be:

keys.put(??value of id??,??not sure what goes here??);

Upvotes: 5

Views: 4221

Answers (10)

Sagar
Sagar

Reputation: 173

I think MultiMap from google library could serve the purpose

https://google.github.io/guava/releases/19.0/api/docs/com/google/common/collect/Multimap.html

    Multimap<String, String> map = ArrayListMultimap.create();
    String key = "uniqueKey";
    map.put(key, "value1");
    map.put(key, "value2");
    map.put(key, "value3");

    System.out.println(map);//{uniqueKey=[value1, value2, value3]}

Upvotes: 0

Jaydev
Jaydev

Reputation: 1812

Create an object that encapsulates the four together. Something like:

public class Foo {
    private String s1;
    private String s2; 
    private int v3;
    private MyObject obj1
    // constructors, getters, helper functions.
}

Upvotes: 0

zengr
zengr

Reputation: 38899

How about HashMap<Integer, ArrayList<String>> ?

UPDATE: Please try to avoid this, this is a better approach.

Upvotes: 1

Javid Jamae
Javid Jamae

Reputation: 9009

No, but the best way is to wrap the information you want to keep in the map in a class:

public class Info {
  private String info;
  private String name;
  private int quantity;

  ...

  public Info(String info, String name, int quantity) {
     ...
  }
}

Then do this to put something in the map:

Info info = new Info("info", "name", 2);
Map map = new HashMap<Integer, Info>();
map.put(22, info);

And do this to get something out:

Info info = map.get(22)

Upvotes: 7

Siri
Siri

Reputation: 207

One of the concerns while using a Map would be use of hardcoded keys. If the key is a string, and the key changes. Can consider using a constant instead of a hardcoded string.

Having a dedicated class has the benefit of compiler to check for name changes. However, as mentioned in the earlier comments.. It can become a concern...

In my opinion both are feasible. We need to weigh which option is better depending on the situation

Upvotes: 0

erikbstack
erikbstack

Reputation: 13254

Sure. Depending on how flexible your datastructe is you can use a Hashmap a la:

  • HashMap<IdType, List<String>>, with IdType String or Integer, depending on the Keys you like to use.
  • HashMap<IdType, String[]>
  • HashMap<IdType, YourObjectType>, with YourObjectType beeing a Object you defined yourself, holding the values you like

YourObjectType can of course be anything you can define as an Object. Also another HashMap if you like.

Upvotes: 0

Stephen C
Stephen C

Reputation: 718906

Not exactly.

A Map defines a strictly 1 to 1 relationship between keys and values. One key in the map has one value.

If you want to associate multiple values with one key you need to do one of the following:

  • Define a Values class to represent the values as a single object; e.g. as per @Starkey's and @Javed's answers. Then the map becomes a Map<String, Values> (assuming that the key type is String).

  • Define the map as a Map<String,List<Object>> or Map<String,Object[]> and represent the values as an untyped list / array

  • Define the map as a Map<String,Properties> or Map<String,Map<String,Object>> and represent the values as the Java equivalent of an associative array.

Of these, the first option is both the safest (smallest chance of runtime errors), the most efficient and the best style.

(Aside: an Apache commons MultiMap might be considered as another possibility, but the conceptual model and APIs don't really match this use-case.)

Upvotes: 0

fastcodejava
fastcodejava

Reputation: 41087

You could have ID as key and a List or Set (Collection in general) of objects as value.

Upvotes: -1

Luxspes
Luxspes

Reputation: 6750

Of course, you could for example declare it like this: HashMap<Integer, HashMap<String,Object>> You use the outer hashmap to link your id with your inner HashMap, and in the inner one, you create keys "info", "name", "quantity" and associate values with them.

Of course, you could also use an ArrayList as the outer collection (it could be a better match for your ID: ArrayList<HashMap<String,Object>> that way you have indexed (id based) access to each of your "info", "name", "quantity" hashmap "records"

Upvotes: -1

Starkey
Starkey

Reputation: 9781

Sure. Make another class that contains your info, name and quantity and put that as the value of your HashMap.

Upvotes: 19

Related Questions