简体   繁体   English

如何从多图获取密钥以获取价值

[英]How to get key from multimap for value

I am having Mutimap which contains following data. 我有Mutimap,其中包含以下数据。

Reports = {UserName1=[report1,report2,report3], UserName2=[report4,report5],UserName3=[report6,report7,report8],UserName4=[report9]}

I want to getkey(report6) for specific value from Multimap, output UserName3 我想从Multimap中获取特定值的getkey(report6) ,输出UserName3

In guava , filterValues with your predicate , you can get the specified entries . guava中使用谓词的 filterValues可以获取指定的条目 Example: 例:

    Multimaps.filterValues(testMultiMap, new Predicate<List<String>>() {
        @Override
        public boolean apply(List<String> strings) {
            return strings.contains("report6");
        }
    });

If you don't preprocess the data, a getkey() query need to scan all values in all keys to get the position. 如果不对数据进行预处理,则getkey()查询需要扫描所有键中的所有值以获取位置。 If you have n querys and the map has m values totally, the algorithm complexity will be O(n*m) . 如果您有n个查询,并且地图总共有m个值,则算法复杂度将为O(n*m)

If you preprocess the data, map the value and the key in a new map, such as newmap['report6'] = 'UserName3' , the algorithm complexity will be O(m+n) . 如果预处理数据,将值和键映射到新映射中,例如newmap['report6'] = 'UserName3' ,则算法复杂度将为O(m+n)

So, when you have many queries, you should choose the second approach. 因此,当您有很多查询时,应该选择第二种方法。

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

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