S
S
Sergey Karbivnichy2018-02-01 18:13:50
Java
Sergey Karbivnichy, 2018-02-01 18:13:50

HashMap - how to get the value?

There is a code:

package com.company;

import java.util.ArrayList;
import java.util.HashMap;

public class Main {


    public static String URL = "url";
    public static String TITLE = "title";

    public static void main(String[] args ){

        ArrayList<HashMap<String ,String>> Sites = new ArrayList<>();
        HashMap<String,String> hashMap;

        hashMap = new HashMap<>();
        hashMap.put(URL,"http://google.com");
        hashMap.put(TITLE,"Google");
        Sites.add(hashMap);

        hashMap = new HashMap<>();
        hashMap.put(URL,"http://yandex.com");
        hashMap.put(TITLE,"Yandex");
        Sites.add(hashMap);

        hashMap = new HashMap<>();
        hashMap.put(URL,"http://Yahoo.com");
        hashMap.put(TITLE,"Yahoo");
        Sites.add(hashMap);


        for(int i = 0; i <Sites.size(); i++ ){

            System.out.println(Sites.get(i));
        }

    }

}

This code returns:
{title=Google, url= http://google.com}
{title=Yandex, url= http://yandex.com}
{title=Yahoo, url= http://Yahoo.com}

But how to do so, to get:
Only the site name and address.
PS: I looked at the examples on https://stackoverflow.com/, but they are a bit confusing there.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Kastian, 2018-02-01
@hottabxp

System.out.println(Sites.get(i).get(URL));
System.out.println(Sites.get(i).get(TITLE));

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question