Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

137
Vistas
How to write method array to Map

I need to write a method that will convert the mass into Map

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

i is an array, so when you put it as the key of your Map, you get a Map<Object[],Object>.

You don't need a nested loop:

public Map<Character, Integer> arrayToMap(Object[][] args) {
    Map<Character, Integer> map = new LinkedHashMap<>();
    for (Object[] i : args) {
        map.put((Character) i[0], (Integer) i[1]);
    }
    return map;
}

Each inner array of your 2D array should become a pair of key and value in the output Map.

over 4 years ago · Santiago Trujillo Denunciar

0

Use Stream API with Callectors.toMap method:

public static Map<Character, Integer> arrayToMap(Object[][] arr) {  
  return Arrays.stream(arr)
               .collect(Collectors.toMap(o -> (Character)o[0],
                                         o -> (Integer)o[1],
                                         (oldV, newV) -> newV,
                                         LinkedHashMap::new
                                         ));
}
over 4 years ago · Santiago Trujillo Denunciar

0

2D array in Java is an array of arrays; i.e. in your example, you have a 1D array of 1D arrays with exactly 2 elements [char, int]

public static void main(String[] args) {
    Object[][] arr = {
            { '1', 1 }, { ' ', 5 }, { '2', 2 }, { '3', 3 },
            { '4', 4 }, { '5', 5 }, { '6', 6 } };

    Map<Character, Integer> map = arrayToMap(arr);
    System.out.println(map);
}

public static Map<Character, Integer> arrayToMap(Object[][] arr) {
    Map<Character, Integer> map = new HashMap<>();

    for (int i = 0; i < arr.length; i++)
        map.put((char)arr[i][0], (int)arr[i][1]);

    return map;
}
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda