Alex Rivera | Logout

Java - Can a hashmap have 4 generic parameters instead of 2?

Asked 2011-08-30T13:40:07.343
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.

Edit
Report

1 Answer

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)));
answered 2011-08-30T13:42:53.257

Your Answer