简体   繁体   English

如何改善HashMap数组中的搜索

[英]How to improve search in an array of HashMap

A search in any HashMap (or any hash based data structure) requires a single hash operation ie O(1). 在任何HashMap(或任何基于哈希的数据结构)中进行搜索都需要单个哈希操作,即O(1)。 However, when we have to search through an array of HashMaps then a single search will requires O(n) hash operations, where n is the size of array. 但是,当我们必须搜索整个HashMap数组时,一次搜索将需要O(n)个哈希运算,其中n是数组的大小。 I was wondering since array is a collections of consecutive memory locations, therefore there could be an efficient method to reduce the search from O(n) to O(1) in array of HashMap. 我想知道数组是连续存储位置的集合,因此可能存在一种有效的方法来减少HashMap数组中从O(n)到O(1)的搜索。 Or we can design some object that in fact have the advantage of array as well as require single hash operation for search. 或者,我们可以设计一些对象,这些对象实际上具有数组的优势,并且需要单个哈希操作进行搜索。 Any suggestion ??? 有什么建议吗?

Consider the scenario, You are processing elements from different users and you want to keep them based on user profiles (separate from each other). 考虑这种情况,您正在处理来自不同用户的元素,并且希望基于用户个人资料(彼此分开)保留它们。 The most memory efficient way is to keep them separate is using array of HashMap. 最内存有效的方法是使用HashMap数组将它们分开。

The answer for this is in your question. 答案在您的问题中。 When you're using an ordinary Array to store these elements, there's no way to get O(1) complexity when searching it. 当使用普通数组存储这些元素时,搜索时无法获得O(1)复杂度。

You stated that the complexity of searching a Hash Map is O(1), so why not store those Hash Maps... in a Hash Map? 您说过,搜索哈希图的复杂度为O(1),那么为什么不将这些哈希图...存储在哈希图中呢? I'll leave the implementation of that up to you, haha. 我将把实现的工作交给您,哈哈。

I've edited your question to include the hypothetical scenario of various user accounts, each with various properties. 我已经编辑了您的问题,以包括各种用户帐户(每个都有不同的属性)的假设情况。 As mentioned in a different answer, the solution here is a map of maps. 就像在另一个答案中提到的那样,这里的解决方案是地图地图。 There are two separate searches occurring: 有两个单独的搜索:

  1. Find the right user 寻找合适的用户
  2. Find the right property of this user 找到该用户的正确属性

Each of these searches can be done with a separate map. 每个搜索都可以使用单独的地图完成。

The user map: 用户图:

class Application {

    ...

    Map<String, User> userMap = new HashMap<>();

    public User getUser(String userName) {
        return userMap.get(userName);
    }

    ...
}

The property map: 属性图:

class User {

    ...

    Map<String, Property> propertyMap = new HashMap<>();

    public Property getProperty(String propertyName) {
        return propertyMap .get(propertyName);
    }

    ...
}

Now to find the property named favoriteTowel of the user named Arthur Dent : 现在查找名为Arthur Dent的用户的名为favoriteTowel的属性:

myApplication.getUser("Arthur Dent").getProperty("favoriteTowel");

Go Go For java.util packages, Go Go for Java Collections 对于java.util包,请转到Go。对于Java集合,请转到Go

look at set ( Hashset , enumset ) and hash ( HashMap , linkedhash ..., idnetityhash ..) based implementations. 看一下基于集合( Hashsetenumset )和哈希( HashMaplinkedhash ..., idnetityhash ..)的实现。 they have O(1) for contains()

Below are stats for reference. 以下是统计数据供参考。

在此处输入图片说明

在此处输入图片说明在此处输入图片说明

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

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