简体   繁体   English

在存储在HashMap中的ArrayList中搜索值

[英]Searching for a value in an ArrayList that is stored in a HashMap

I am trying to figure out how to retrieve a value that is stored in an ArrayList, stored in a Hashmap. 我试图弄清楚如何检索存储在Hashmap中的ArrayList中存储的值。

Here's what I have: 这是我所拥有的:

HashMap<String, ArrayList<Record>> records = new HashMap<>();

The key in this hashmap is not what I am looking for. 该哈希图中的关键不是我想要的。 There is a method inside of the Record object called getRecordId() and I want to be able to evaluate whether or not this recordId , through an if statement, exists in the ArrayList. 有一个名为Record对象里面的方法getRecordId()我希望能够评估该与否recordId ,通过if语句,存在于ArrayList中。

Example: 例:

if(records.values.exists(recordId)){ ...do something ...}

Essentially, I want to loop through all the values in the ArrayList to see if that record ID exists, and if it does, I will store the key and compute some things. 本质上,我想遍历ArrayList中的所有值以查看该记录ID是否存在,如果存在,我将存储键并计算一些内容。 How to I do this? 我该怎么做?

Edit: right after posting this question, I think I am on to something. 编辑:发布此问题后,我想我正在处理一些事情。 How about this: 这个怎么样:

Set<Map.Entry<String, ArrayList<Record>>> entrySet = records.entrySet();
for(Map.Entry<String, ArrayList<Record>> data : entrySet)
{
    for(Record entry : data.getValue())
    {
        if(recordId.equals(entry.getRecordId()))
        {
            // Do something here
            return "";
        }
    }
}

I need to leave the loops if the record ID has been found, because record IDs are unique. 如果找到记录ID,我需要离开循环,因为记录ID是唯一的。

You can use here Hashmap.entrySet() to get the list of all the keys, and iterate through that key set, and check if recordId exists in the ArrayList for that particular iteration, then store the key and do your computations. 您可以在此处使用Hashmap.entrySet()获取所有键的列表,并迭代该键集,并检查该特定迭代的ArrayList中是否存在recordId ,然后存储该键并进行计算。

If you are not sure about the syntax and usage of entrySet then you can look into it here - https://www.geeksforgeeks.org/iterate-map-java/ 如果你不能确定的语法和用法entrySet那么你可以考虑在这里- https://www.geeksforgeeks.org/iterate-map-java/

You can use the following syntax : 您可以使用以下语法:

records.forEach((k, v) -> {
    if (v.contains(recordId)) {
        // do something with 'k'
    }
});

Here we are iterating over the map using forEach . 在这里,我们使用forEach遍历地图。 The k stands for the key and the v stands for the value. k代表键, v代表值。 You can also achieve the same using the entrySet 您也可以使用entrySet实现相同的功能

records.entrySet().forEach(e -> {
    if (e.getValue().contains(recordId)) {
        // do something with 'e.getKey()'
    }
});
for (Map.Entry<String, ArrayList> data : records) {
            ArrayList list = (ArrayList) data.getValue();
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i).equals(recordId)) {
                    // do something
                }
            }
        }

If you are using Java 8 you can use stream : 如果您使用的是Java 8 ,则可以使用stream

//List of keys which contains your desired Record at it's value(ArrayList)
List<String> requiredListOfKeys = records.keySet().stream()
                                                  .filter(x -> records.get(x)
                                                  .contains(record))
                                                  .collect(Collectors.toList());

One way to go would be to stream the map values, and for each value (which is a list), stream it and find the first Record instance that matches your search criteria. 一种方法是流式传输地图值,并为每个值(这是一个列表)流式传输地图值,并找到第一个与您的搜索条件匹配的Record实例。 Then, if such record was found, do whatever you want to do with it: 然后,如果找到了这样的记录,请执行以下操作:

boolean found = records.values().stream()
    .flatMap(List::stream) // or .flatMap(list -> list.stream())
    .filter(entry -> recordId.equals(entry.getRecordId()))
    .findFirst()
    .ifPresent(entry -> {
        // do something with the found record
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM