java.util.NoSuchElementException - 迭代已排序的treemap [英] java.util.NoSuchElementException - on iterating over sorted treemap

查看:303
本文介绍了java.util.NoSuchElementException - 迭代已排序的treemap的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在循环我的程序,并且由于某些原因我没有出现以下错误

I'm looping over my program, and for no certain reasons I've got the following error

Exception in thread "main" java.util.NoSuchElementException
    at java.util.TreeMap$PrivateEntryIterator.nextEntry(Unknown Source)
    at java.util.TreeMap$KeyIterator.next(Unknown Source)
    at EloRating.setNewSeason(EloRating.java:242)

此错误指向迭代器循环。

This error points to the iterator loop.

TreeMap<String,Double> treeMap = new TreeMap<String,Double>();
                                    for (Entry<String, Double> team : teamsIntersection.entrySet()) {
                                        treeMap.put(team.getKey(), listTeams.get(team.getKey()).getRating());
                                        }

                                    SortedSet<Entry<String, Double>> listSorted = entriesSortedByValues(treeMap);
                                    Iterator<Entry<String, Double>> iter = listSorted.iterator();

                                    int stop = (amountOfIntersectTeams+1)/2;
                                    for(int i = 0; i < (stop-1); i++){
                        ----------->   iter.next();
                                    }
                                    double median = iter.next().getValue();

如果我执行一次程序,没有什么特别的事情发生。但是出于一些基准原因,我将通过一些略微调整的参数循环程序25.000次。
当我尝试使用错误后的参数再次启动程序时,它会更进一步。 [在此错误时刻,迭代器包含10个值]

If I execute the program once, nothing special happens. But for some benchmark reasons I've to loop over the program 25.000 times with some slightly adjusted parameters. And when I try to start the program again with the parameters from after an error, it will go further. [At the moment of this error, iterator contained 10 values]

我想要上层Hashmap teamsIntersection值的中位数。
,例如a-3 b-1 c-2 d-2 e-5 f-4然后我希望从

entriesSortedByValue

$ b返回3的值
$ b

I want to have the upper median on the values of the Hashmap teamsIntersection. e.g. a-3 b-1 c-2 d-2 e-5 f-4 then I want the value of 3 back from a

static <K,V extends Comparable<? super V>>
    SortedSet<Map.Entry<K,V>> entriesSortedByValues(Map<K,V> map) {
        SortedSet<Map.Entry<K,V>> sortedEntries = new TreeSet<Map.Entry<K,V>>(
            new Comparator<Map.Entry<K,V>>() {
                @Override public int compare(Map.Entry<K,V> e1, Map.Entry<K,V> e2) {
                    return e1.getValue().compareTo(e2.getValue());
                }
            }
        );
        sortedEntries.addAll(map.entrySet());
        return sortedEntries;
    }


推荐答案

如果您有多个条目相同的值,它们将通过您编写的比较器得到合并,因为 TreeSet 相对于其比较器扣除元素。

If you have multiple entries with the same value, they will get merged by the comparator you've written, since TreeSet dedups elements relative to its comparator.

假设 amountOfIntersectTeams 是您正在处理的地图的大小,那么这是我最好的选择。

Assuming that amountOfIntersectTeams is the size of the map you're dealing with, that's my best bet as to what's going on.

要修复它,我会考虑在值比较相等时添加比较器中键的二次比较。

To fix it, I'd consider adding a secondary comparison of the keys in your Comparator when the values compare as equal.

这篇关于java.util.NoSuchElementException - 迭代已排序的treemap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆