从Hashmap获取包含所需信息的对象?

时间:2018-05-10 17:46:59

标签: java hashmap

我有{% if addresses|length != 0 %} {% set counter= 0 %} {% for address in addresses %} <input value="{{ address }}"> <p>{{balances[counter]|number_format(10, '.', ',')}} BTC / 0.00 USD</p> {% set counter= counter+1 %} {% endfor %} {% else %} <p>No addresses found!</p> {% endif %} HashMap<String, Object>包含以下信息:Objectnamesurname。如何仅获取包含示例的对象:特定unique ID

2 个答案:

答案 0 :(得分:0)

使用姓氏作为键

将对象存储在HashMap中
HashMap<String, List<MyObject>> map = new HashMap<>();
map.putIfAbsent(myobj.surname, new List<MyObject>();
map.put(myobj.surname, map.get(myobj.surname).add(myobj));

map.get("surname");

答案 1 :(得分:0)

尝试过滤您的列表:

HashMap<String, YourObject> input =...;

如果你想要一个对象列表:

List<YourObject> result = input.values().stream()
              .filter(o -> o.getSurname().equals("your filter surname"))
              .collect(Collectors.toList());

如果你想要一个对象的hashmap

Map<String, YourObject> result = input.entrySet().stream()
              .filter(e -> e.getValue().getSurname().equals("your filter surname"))
              .collect(Collectors.toMap(Entry::getKey, Entry::getValue));