Help to store this data

1

I am interested in saving two types of data, String and int. I have to store numbers so that:

int: 1/ String:"Uno"
...
int: 89/ String:"Ochenta y nueve"

I know that in other languages there are tuples, but I do not know how to store it in java. Later I need to access the int and the string.

    
asked by hugoboss 26.05.2017 в 13:17
source

2 answers

1

If the number is the key you can use a HashMap.

Map<Integer, String> mapaDatos = new HashMap<Integer, String>();
mapaDatos.put(1, "Uno");
mapaDatos.put(2, "Dos");
// Asi hasta el ultimo
    
answered by 26.05.2017 / 13:22
source
0

You can implement it in several ways I will leave you the two main ones, first you must make the import of the following Classes:

import java.util.Map;
import java.util.TreeMap;
import java.util.HashMap;

Then you must create your Map and fill it with the data.

//indicamos que utilizaremos un Map con Clave Integer y valor String, esta vez utilizaremos la implementacion HashMap el cual guarda los datos como si fuera una tabla.
Map<Integer,String> miMapa = new HashMap<>();        
miMapa.put(10, "diez");
miMapa.put(5, "cinco");
miMapa.put(3, "tres");

//indicamos que utilizaremos un Map con Clave Integer y valor String,esta vez utilizaremos otra implementacion (TreeMap) el cual tiene una estructura de arbol.
Map<Integer,String> miMapa = new TreeMap<>(); 
miMapa.put(10, "diez");
miMapa.put(5, "cinco");
miMapa.put(3, "tres");

//recorremos el Map de la siguiente forma indicamos el tipo de llave
En este caso un Integer seguido de dos puntos (:) seguido de la variabla miMapa.keySet() el cual nos retorna todas las llaves del Map
for(Integer llave : miMapa.keySet())
{
   //imprimimos la llave y su valor con el metodo get() , el cual recibe la llave.
   System.out.println(llave + " ---> " + miMapa.get(llave));
}

I hope it serves you Greetings.

    
answered by 27.05.2017 в 01:58