I can not access a specific value of an item in java

1

Good morning,

I need to access the value within a java item. I start by initializing a ArrayList<Item> in the following way:

ArrayList<Item> games = MicrosoftStore.TodaInfoJuegos(countries, urls);

That arrayList I go through with a foreach loop, since it is composed of items that have the following structure (it's an example):

Name: DEAD OR ALIVE 5 Last Round Character: Zack
Type: ---
Descripcion Castellano: Descarga este DLC para poder usar a Zack en la versi&oacute;n gratuita de DEAD OR ALIVE 5 Last Round: Luchadores b&aacute;sicos. Este DLC podr&iacute;a ofrecerse como prueba gratuita durante un tiempo limitado en el futuro.
Descripcion Inglés: Download this to use Zack in DEAD OR ALIVE 5 Last Round: Core Fighters.
Descripcion Pegi: Online Interactions Not Rated by the ESRB; Partial Nudity; Sexual Themes; Violence; 
Edad Pegi: 16+
Edad Pegi USA: Mature 17+
Microsoft Store ID: c3lcm7jmcs4q
Microsoft Store URL: c3lcm7jmcs4q

So I go through the ArrayList:

for (Item game : games) {
    String code = game.get(Constants.VALUE_MSSTORE_URL);
    //código en el que trabajo con la variable code
}

The fact is that every time I go through the foreach loop I want to access the value of Microsoft Store ID but I am unable.

The value of Constants.VALUE_MSSTORE_URL is "Microsoft Store URL" , simply that I have it in another different class and I call it that way.

What am I doing wrong?

    
asked by JetLagFox 30.04.2017 в 15:52
source

1 answer

1

I think you're in need of a HashMap , that's what it is usually used in Java to store related values by key - > value, as is the case you sample. The even values are entered in the map, once created, using the put method. Maps do not support duplicate keys, that is, the values on the left can not be duplicated. You can see more details in the Java documentation .

In the example, you will see how to read a value by its code name. And you'll see two ways to read the entire map, before and after Java 8.

You can adapt your Map to any class, enter its values in arrays, in the database or wherever you want:

VIEW DEMO

Code

import java.util.*;
import java.lang.*;

class Rextester
{  
    public static void main(String args[])
    {
         String VALUE_MSSTORE_URL = new String ("Microsoft Store URL");
         Map<String, String> map = new HashMap<String, String>();
         map.put("Name", "DEAD OR ALIVE 5 Last Round Character: Zack");
         map.put("Type", "---");
         map.put("Descripcion Castellano", "Descarga este DLC para poder usar a Zack en la versión gratuita de DEAD OR ALIVE 5 Last Round...");
         map.put("Descripcion Inglés", "Download this to use Zack in DEAD OR ALIVE 5 Last Round: Core Fighters.");
         map.put("Descripcion Pegi", "Online Interactions Not Rated by the ESRB; Partial Nudity; Sexual Themes; Violence;");
         map.put("Edad Pegi", "16+");
         map.put("Edad Pegi USA", "Mature 17+");
         map.put("Microsoft Store ID", "c3lcm7jmcs4q");
         map.put("Microsoft Store URL", "c3lcm7jmcs4q");

         System.out.println("\nBUSCANDO VALOR POR NOMBRE DE KEY\n");   

         System.out.println(map.get(VALUE_MSSTORE_URL));

         System.out.println("\nLEYENDO TODO EL MAPA: ANTES DE JAVA 8\n");
             for (Map.Entry<String, String> entry : map.entrySet()) 
         {
              System.out.println("Key : " + entry.getKey() + " Value : " + entry.getValue());
         }


         System.out.println("\nLEYENDO TODO EL MAPA: JAVA 8\nforEach y lambda\n");

         map.forEach((k,v)->System.out.println("Key : " + k + " Value : " + v));

    }
}

Result

BUSCANDO VALOR POR NOMBRE DE KEY

c3lcm7jmcs4q

LEYENDO TODO EL MAPA: ANTES DE JAVA 8

Key : Descripcion Pegi Value : Online Interactions Not Rated by the ESRB; Partial Nudity; Sexual Themes; Violence;
Key : Edad Pegi USA Value : Mature 17+
Key : Type Value : ---
Key : Descripcion Inglés Value : Download this to use Zack in DEAD OR ALIVE 5 Last Round: Core Fighters.
Key : Microsoft Store ID Value : c3lcm7jmcs4q
Key : Edad Pegi Value : 16+
Key : Microsoft Store URL Value : c3lcm7jmcs4q
Key : Descripcion Castellano Value : Descarga este DLC para poder usar a Zack en la versión gratuita de DEAD OR ALIVE 5 Last Round...
Key : Name Value : DEAD OR ALIVE 5 Last Round Character: Zack

LEYENDO TODO EL MAPA: JAVA 8
forEach y lambda

Key : Descripcion Pegi Value : Online Interactions Not Rated by the ESRB; Partial Nudity; Sexual Themes; Violence;
Key : Edad Pegi USA Value : Mature 17+
Key : Type Value : ---
Key : Descripcion Inglés Value : Download this to use Zack in DEAD OR ALIVE 5 Last Round: Core Fighters.
Key : Microsoft Store ID Value : c3lcm7jmcs4q
Key : Edad Pegi Value : 16+
Key : Microsoft Store URL Value : c3lcm7jmcs4q
Key : Descripcion Castellano Value : Descarga este DLC para poder usar a Zack en la versión gratuita de DEAD OR ALIVE 5 Last Round...
Key : Name Value : DEAD OR ALIVE 5 Last Round Character: Zack
    
answered by 01.05.2017 / 06:00
source