Using a hashMap

Asked

Viewed 12,987 times

0

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

How do I know if an element is in this list via the 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);

Other class...

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

}
  • 2

    Can you explain it better with a code example? By the way, the code is Java or C#?!

  • The code is in java. I will try to explain my question better.

  • 1
    1. your code compiles? I believe not, because here filter.put("carteiras", carteiras) you are trying to put a list of wallets where should be a Wallet object, or the Map statement should have been Map<String, List<Carteira>> filter = new HashMap<>(); 2) what other class? how do you want to call a method of another class? 3) Como faço para pegar o objeto carteira1 which criterion you want to use to know which element Voce wants to take? maybe Voce wants to pass the object portfolio as argument to the method buscarCarteira() tb?
  • i already manage to pass as parameter the list inside the map and already get to the method, it is coming the values correctly, only I want to know how to access each element of that list that is in the map.

  • param.get("carteiras").containsKey(carteira1)

  • that’s my question, I don’t know how to pick up the value inside that list.

Show 1 more comment

3 answers

5


Answering the question:

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

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

I created a compileable 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 execute only once, because within the variable param has only a couple of values "carteiras", carteiras.

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

  • I thought there was some method of map ownership that did that. And I did that only it was with two arraylist inside each other. If I didn’t have this property I already and to do this, but it was good you clarify me, vlw a lot!!!

-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;
    }

}

-1

So...

Any field within the Wallet Object that is unique?

Why create a list and add it to a map? You couldn’t add it directly 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
    }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.