Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

hashmap

hash

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.

like image 343
john Avatar asked Aug 30 '11 13:08

john


1 Answers

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)));
like image 150
oliholz Avatar answered Oct 07 '22 02:10

oliholz