Using a hashMap

0

I have a hashMap and inside it I have an ArrayList.

How do I know if an element has this list through hashMap?

List<Carteira> carteiras = ArrayList<Carteira>();
carteiras.add(carteira1);
carteiras.add(carteira2);

Map<String, Carteira> filter = HashMap<String, Carteira>();
filter.put("carteiras", carteiras)

buscarCarteiras(filter);

Another class ...

buscarCarteiras(Map<String,Carteira> param){
    //Como faço para pegar o objeto carteira1

}
    
asked by anonymous 11.03.2014 / 01:52

3 answers

5

Answering the question:

  

I want to know how to access each element of that list that is in the map

You should make two loops, one that scrolls through all the elements of your Map and another that scrolls through all the elements of the List, which is inside your Map.

I've created a compiling example that demonstrates something similar to your original question and answering your question that is in the comment:

import java.util.*;

class Carteira {
    private int num;
    public void setNum(int num) { this.num = num; }
    public int getNum() { return this.num; }
    @Override
    public String toString() { return "Valor da carteira: " + num; }
}

public class TesteHash {
    public static void main(String[] args) {
        Map<String, List<Carteira>> filter = new HashMap<>();
        List<Carteira> carteiras = new ArrayList<>();
        Carteira carteira1 = new Carteira();
        Carteira carteira2 = new Carteira();

        carteira1.setNum(10);
        carteira2.setNum(15);
        carteiras.add(carteira1);
        carteiras.add(carteira2);
        filter.put("carteiras", carteiras);

        buscarCarteiras(filter);
    }

    public static void buscarCarteiras(Map<String, List<Carteira>> param) {
        //aqui responde a sua dúvida
        for(Map.Entry<String, List<Carteira>> entry: param.entrySet()) { 
            for(Carteira c: entry.getValue()) {
                //na variavel 'c' vc tem um objeto carteira
                System.out.println(c);
            }
        }
    }
}

Note that the first for will only execute once, because within the variable param it has only one pair of values "carteiras" , carteiras .

The second for will be executed twice, since it runs through the entire list that is inside the Map, and within that list has two objects of type Carteira . Within this for it will print the value that returns from% method of% of class toString() .

    
11.03.2014 / 03:21
-1
package com.teste.apl;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Main {

    static Map<Integer, String> mapaNomeClientes = new HashMap<>();
    public static void main(String[] args) {


        mapaNomeClientes.put(1, "Willian Marques");
        mapaNomeClientes.put(2, "Antonio Dias");
        mapaNomeClientes.put(3, "Ortiz");
        mapaNomeClientes.put(1, "Teste");

        System.out.println(pesquisar());

    }
    private static String pesquisar(){
        Scanner entrada = new Scanner(System.in); 
        System.out.println("Informe a key");
        Integer key = entrada.nextInt();
        if(mapaNomeClientes.containsKey(key)){
            return String.valueOf(mapaNomeClientes.get(key));
        }
        return null;
    }

}
    
07.05.2016 / 06:41
-1

Then ...

Any field within the Portfolio Object that is unique?

Why create a list and add it to a map? you could not add direct to a map EX

Map<String,Carteiras> mapa=new HashMap<>();

    Carteira carteira=new Carteira();

    // setar campos
    mapa.put(carteira.getId(),carteira);

    if(mapa.containskey("00X1")) {
        /// get da carteira
    }

Why not use lambda with java and List

List<RealVar> lista=new ArrayList<>();

    Optional<RealVar> optional=lista.stream().filter(x->x.getId()==19).findFirst();

    if(optional.isPresent()) {
        // faça o que Precisa
    }
    
12.09.2018 / 02:09