2011-09-29 19 views
12

soy nuevo en Java. ¿Cómo puedo almacenar una matriz de valores enteros en un HashMap? Después de eso, escribo este HashMap en un archivo txt, pero esto no es importante por el momento. Puedo almacenar campos individuales pero no una matriz. Algunas ideas ?Almacenar una matriz en HashMap

public void salveazaObiectulCreat(String caleSpreFisier) { 

    HashMap map = new HashMap(); 

    map.put ("Autorul",numelePrenumeleAutorului); 
    map.put ("Denumirea cartii",denumireaCartii); 
    map.put ("Culoarea cartii",culoareaCartii); 
    map.put ("Genul cartii",gen); 
    map.put ("Limba",limba); 
    map.put ("Numarul de copii",numarulDeCopii); 
    map.put ("Numarul de pagini",numarulDePagini); 
    map.put ("Pretul cartii",pretulCartii); 

    try { 

     File file = new File(caleSpreFisier); 

     FileOutputStream f = new FileOutputStream(file); 

     ObjectOutputStream s = new ObjectOutputStream(f);   

     s.writeObject(map); 

     s.close(); 

     } catch(Exception e){ 

      System.out.println("An exception has occured");  
    } 
} 
+1

¿Una matriz de qué enteros? ¿Estás tratando de poner múltiples arrays en HashMap? –

+0

No debería haber diferencia entre arrays y cualquier otro objeto. ¿Qué intentaste? – biziclop

+0

esos valores en map.put son cadenas, ahora quiero almacenar una matriz de valores int (algunos dígitos) –

Respuesta

18
HashMap<String, List<Integer>> map = new HashMap<String, List<Integer>>(); 
HashMap<String, int[]> map = new HashMap<String, int[]>(); 

escoger uno, por ejemplo

HashMap<String, List<Integer>> map = new HashMap<String, List<Integer>>(); 
map.put("Something", new ArrayList<Integer>()); 
for (int i=0;i<numarulDeCopii; i++) { 
    map.get("Something").add(coeficientUzura[i]); 
} 

o simplemente

HashMap<String, int[]> map = new HashMap<String, int[]>(); 
map.put("Something", coeficientUzura); 
0

Si desea almacenar varios valores para una clave (si he entendido bien), usted podría intentar un MultiHashMap (disponible en varias bibliotecas, no sólo los bienes comunes colecciones).

21
No

seguro de la pregunta exacta, pero es esto lo que está buscando?

public class TestRun 
{ 
    public static void main(String [] args) 
    { 
     Map<String, Integer[]> prices = new HashMap<String, Integer[]>(); 

     prices.put("milk", new Integer[] {1, 3, 2}); 
     prices.put("eggs", new Integer[] {1, 1, 2}); 
    } 
} 
0

Su vida será mucho más fácil si puede guardar una Lista como valor en lugar de una matriz en ese Mapa.

6

Sí, la interfaz del Mapa le permitirá almacenar Arrays como valores. Aquí hay un ejemplo muy sencillo:

int[] val = {1, 2, 3}; 
Map<String, int[]> map = new HashMap<String, int[]>(); 
map.put("KEY1", val); 

También, dependiendo de su caso de uso es posible que desee ver en el apoyo ofrecido por Multimapguava.

-1

Puede almacenar objetos en un HashMap.

HashMap<String, Object> map = new HashMap<String, Object>();

Usted sólo tendrá que emitir de nuevo correctamente.

Cuestiones relacionadas