简体   繁体   English

Java遍历hashmap 2级

[英]Java iterate over hashmap 2 levels

I have this class: 我有这个课:

    public class docInfo {
        private int freq;
        private HashMap<String, Double> m = new HashMap<String, Double>();
    }

    private Map<String, docInfo> m;

I want to iterate over all the docInfo's Double value and change it: 我想遍历所有docInfo的Double值并更改它:

docInfo documento;

 for (Map.Entry<String, docInfo> entry : m.entrySet()) {
            word = entry.getKey();            
            docs = entry.getValue(); // docs map


            for (Map.Entry<String, Double> entry2 : docs.getM().entrySet()){  

                score = entry2.getValue(); 
                temp = score;

                double log = Math.log10(docs.getFreq());
                double tfw = log+cons;

                docs.changeScoreTo(entry2.getKey(), tfw);
                //entry2.setValue(tfw);

        }
        }

It is working, the problem is, it is taking too long to run and i think my code is not the fittest for the job. 它正在工作,问题是,它花费的时间太长,我认为我的代码不是最合适的工作。 Any help would be appreciated, 任何帮助,将不胜感激,

You really should use the profiler to understand why code is slow. 确实应该使用探查器来了解为什么代码很慢。 It's better than guessing. 比猜测更好。 If I had to guess, though, I'd say the problem is the very large maps (27K) are slow to build and retrieve. 不过,如果我不得不猜测,我会说问题是很大的地图(27K)生成和检索都很慢。 The default hash tables are much too small for 27K entries and so you are doing a lot of rehashing as you assemble. 默认的哈希表对于27K条目来说太小了,因此在组装时要进行大量的哈希处理。

Try 尝试

private HashMap<String, Double> m = new HashMap<String, Double>(5000);

And try a profiler to let us know which lines eat cycles. 并尝试使用探查器让我们知道哪些行会吃掉周期。

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

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