Home > Back-end >  Get value from ArrayList object in Java?
Get value from ArrayList object in Java?

Time:02-13

I know this is a quite basic question, but after reading some SO posts and google I couldn't understand the issue well, so.

I have a map object data, a System.out.println(data) shows me something like the following on the console:

{data=[{engine=engine1, keyword=keyword1, url=url1}, {engine=engine2, keyword=keyword2, url=url2}]}

Also, a data shows me LinkedHashMap@11 size=1 on the console.

Now I wanted to have the first "engine" key value of the first array of "data", aka engine1.

Before that I tried to obtain the first of the array, so I tried the following, but I get errors.

data.get("data")[1]
Cannot evaluate because of compilation error(s): The type of the expression must be an array type but it resolved to Object.

data.get("data").get(1)
Cannot evaluate because of compilation error(s): The method get(int) is undefined for the type Object.

How can I obtain the "engine1" value from "data"? Thanks for your help.

edit:

The code around it is as shown below. data is declared by Map<String, Object> data.

import org.yaml.snakeyaml.Yaml;

~~ 

InputStream inputStream = new FileInputStream(new File("student.yml"));

Yaml yaml = new Yaml();
Map<String, Object> data = yaml.load(inputStream);
System.out.println(data);

(not sure you need this info but just in case, "student.yml" file is as shown below)

data: 
  - engine: engine1
    keyword: keyword1
    url: url1
  - engine: engine2
    keyword: keyword2
    url: url2

CodePudding user response:

Updating answer post question edit; Please find below, the solution you want;

import org.yaml.snakeyaml.Yaml;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

public class YamlParser {

    public static void main(String[] args) throws FileNotFoundException {

        InputStream inputStream = new FileInputStream(new File("src/main/resources/student.yml"));

        Yaml yaml = new Yaml();
        Map<String, Object> data = yaml.load(inputStream);
        System.out.println(data); // Prints full map
        List<Map<String,String>> eng = (List<Map<String,String>>) data.get("data"); //Casting object to List
        System.out.println(eng.get(0).get("engine")); // Prints the value : engine1
    }
}

CodePudding user response:

I think this code is also help you.

import java.util.*;
public class  Main{    
public static void main(String[] args) {  
    ArrayList<Dictionary> data=new ArrayList<Dictionary>();  
    Dictionary geek = new Hashtable();
    geek.put("engine","engine1");
    geek.put("keyword","keyword1");
    geek.put("url","url1");
    data.add(geek);
 
    Dictionary geek1 = new Hashtable();
    geek1.put("engine","engine2");
    geek1.put("keyword","keyword2");
    geek1.put("url","url2");
    data.add(geek1);
    
    System.out.println(data);
    System.out.println(data.get(0).get("engine"));
}
}
  •  Tags:  
  • java
  • Related