Treemap排序的位置访问



如果我想检查三核中第一个索引位置的值是否为xyz,然后做xyz否则,如果第一个索引位置存在的值是ABC,则进行ABC。我将如何写这个?因为我想访问Treemap中安排的索引。我想按顺序访问Treemap的排序键。需要帮助

//this code iterates through all the keys in sorted order
treeMap.keySet().forEach(key -> {
    //use key to do whatever you need
});

编辑

//this code iterates through all entries (from first, second, third, ... to last)
tree.entrySet().forEach(entry -> {
    key = entry.getKey();
    value = entry.getValue();
    //use key and value to do whatever you need
});

edit2

//this codes finds the first key whose value equals the desired value
Object key = tree.entrySet().stream()
                            .filter(e -> e.getValue().equals(desiredValue))
                            .map(Entry::getKey)
                            .findFirst()
                            .get();

最快,最丑陋的黑客可能只是

List<KeyType> keyList = new ArrayList<>(myTreeMap.keySet());
KeyType nthKey = keyList.get(nthIndex);

但是我要问自己为什么当我想通过索引查找条目时,我首先要使用TreeMap?使用List并致电sort

会更有效

最新更新