2011-08-30 8 views
9

Questo può essere difficile da spiegare, ma qui va:Java: una hashmap può avere 4 parametri generici invece di 2?

voglio memorizzare 3 numeri interi e una stringa in un HashMap, così posso recuperare i dati dalla mappa, ma si scopre che HashMaps permettono solo 2 parametri generici invece di 4.

per esempio: HashMap <String> <Integer> <Integer> <Integer> (cosa che voglio fare)

ma è possibile utilizzare solo 2 parametri, come sembra: HashMap <String> <Integer>.

La mia ipotesi migliore è che la mia idea non può essere fatta, in tal caso, si prega di elencare le alternative alla gestione di qualcosa di simile.

+0

cosa stai cercando di mappare? la stringa a tutti gli interi? – amit

+1

Per chiarimenti, ti riferisci ai ** parametri generici di HashMap **, che sono due, K e V. In realtà esistono 4 ** costruttori ** su [HashMap] (http://download.oracle.com/ javase/6/docs/api/java/util/HashMap.html), tuttavia. – Jeremy

risposta

13

Creare una nuova classe che contenga 3 Integer o forse 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; 
    } 
} 

e inserire questa classe in una mappa con String.

HashMap map = new HashMap<String, Triple>(); 
map.put("keyString", new Triple(new Integer(1),new Integer(2),new Integer(3))); 
+0

+1 ma non dovrebbe essere scritto Triple? – Davy8

+0

di causa- Tripla – oliholz

3

È necessario creare un oggetto per contenere tali dati, quindi archiviarli in questo modo: HashMap<String, MyObject>.

Inoltre, questi non sono costruttori. Sono generici.

0

Mi sembra che si stia tentando di memorizzare due diversi tipi di cose come valori nella mappa hash. Non ci sono problemi nel farlo. Basta creare la mappa hash con il costruttore predefinito e quindi utilizzare semplicemente Object come tipo di valore. così new HashMap<String, Object>()

1

È possibile ottenere la risposta indiretta, come comporre la stringa di tre numeri interi a un carattere,

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("_"); 

Quindi è possibile accedere ai valori interi.

0

È possibile utilizzare una HashMap < TypeOfYourKey, Object> per memorizzare oggetti arbitrari.

3

Non è necessario un hashmap per memorizzare 4 valori. Per memorizzare 3 numeri interi e 1 stringa:

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

Ho lottato con questo stesso problema. Ho finito per creare una hashmap di una classe personalizzata. Questo ha funzionato pienamente e mi ha permesso di inserire tutti gli attributi che desideravo nella mia classe e di estrarre gli attributi per ogni oggetto a livello di codice. Esempio completo di seguito.

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

} 
Problemi correlati