11

This may be difficult to explain, but here goes:

I want to store 3 integers and a String to a Hashmap, so I can retrieve data from the map, but it turns out that hashmaps only allow 2 generic parameters instead of 4.

For example: HashMap <String> <Integer> <Integer> <Integer> (what I want to do)

but you can only use 2 parameters, as it seems: HashMap <String> <Integer>.

My best guess is that my idea cannot be done, if so please list the alternatives to handling something like this.

2
  • what exactly are you trying to map? the String to all integers?
    – amit
    Aug 30, 2011 at 13:43
  • 1
    For clarification, you are referring to the HashMap's generic parameters, which there are two, K and V. There actually are 4 constructors on HashMap, however.
    – Jeremy
    Aug 30, 2011 at 14:15

7 Answers 7

16

Make a new class which holds 3 Integer or maybe int.

class Triple {
    Integer i;
    Integer j;
    Integer k;

    Triple(Integer i,Integer j, Integer k) {
        this.i = i;
        this.j = j;
        this.k = k;
    }
}

and put this class to a map with the String.

HashMap map = new HashMap<String, Triple>();
map.put("keyString", new Triple(new Integer(1),new Integer(2),new Integer(3)));
2
  • Using the approach mentioned above how would you get the values for the keySet? System.out.println(map.values()) will print out something like this utils.Triple@5e1fc2aa
    – Daredevi1
    Dec 9, 2020 at 16:52
  • map.values() gives you a Collection<Triple>. For each Triple you can access i,j,k directly or you can provide a getter Method. If you want to print the Triple you can override the public String toString() method.
    – oliholz
    Jan 7, 2021 at 9:58
3

You should create an object to hold that data, and then store it like this: HashMap<String, MyObject>.

Also, these aren't constructors. They are generics.

3

You don't need a hashmap to store 4 values. To store 3 integers and 1 String:

public class MyClass {
  int a,b,c;
  String d;
}
1

You can get the answer indirectly, like composing the three integer to one character string,

int val1=1;
int val2=2;
int val3=3;
Map<String,String> test = new HashMap<String,String>();
test.put("key1", val1+"_"+val2+"_"+val3);
when you wan to get the values, int[] rst = test.get("key1).split("_");

Then you can access your integer values.

0

It seems to me that you are trying to store two different types of things as values in the hash map. There is no problem in doing this. Just create the hash map with the default constructor, and then just use Object as the value type. so new HashMap<String, Object>()

0

You can use a HashMap< TypeOfYourKey, Object > to store arbitrary objects.

0

I struggled with this same issue. I ended up creating a hashmap of a custom class. This fully worked, and allowed me to put whatever attributes I wanted in my class, and pull out those attributes for any item programmatically. Full example below.

public class Test1 {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.addview);


//create the data mapping
    HashMap<Integer, myClass> hm = new HashMap<Integer, myClass>();
    hm.put(1, new myClass("Car", "Small", 3000));
    hm.put(2, new myClass("Truck", "Large", 4000));
    hm.put(3, new myClass("Motorcycle", "Small", 1000));



//pull the datastring back for a specific item.
//also can edit the data using the set methods.  this just shows getting it for display.
    myClass test1 = hm.get(1);
    String testitem = test1.getItem();
    int testprice = test1.getPrice();
    Log.i("Class Info Example",testitem+Integer.toString(testprice));

}

}








class myClass{
    private String item;
    private String type;
    private int price;


    public myClass(String itm, String ty, int pr){
        this.item = itm;
        this.price = pr;
        this.type = ty;
    }

    public String getItem() {
        return item;
    }

    public void setItem(String item) {
        this.item = item;
    }

    public String getType() {
        return item;
    }

    public void setType(String type) {
        this.type = type;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.