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()
.