Java 中 .NET SortedDictionary 的等价物是什么?

发布于 2024-10-10 18:01:55 字数 383 浏览 0 评论 0原文

如果 .NET 有一个 SortedDictionary 对象...这在 Java 中是什么, 请?我还需要能够在 Java 代码中检索(元素的)Enumeration ..这样我就可以迭代所有键。

我认为这是一个 TreeMap ?但我不认为有一个暴露的 Enumeration

有什么想法吗?

If .NET has a SortedDictionary object ... what is this in Java, please? I also need to be able to retrieve an Enumeration (of elements), in the Java code .. so I can just iterate over all the keys.

I'm thinking it's a TreeMap ? But I don't think that has an Enumeration that is exposed?

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

怪我入戏太深 2024-10-17 18:01:55

TreeMap 将是正确的选择。至于所有键(或值)的集合,任何Map都会公开keySet()values()

编辑(用代码标签回答您的问题)。假设您有一个 Map

for (String key : map.keySet()) {
     System.out.println(key); // prints the key
     System.out.println( map.get(key) ); // prints the value
}

您还可以使用 entrySet() 而不是 keySet()values( ) 以便迭代键->值对。

TreeMap would be the right choice. As for the Collection of all the keys (or values), any Map exposes keySet() and values().

EDIT (to answer your question with code tags). Assuming you have a Map<String, Object>:

for (String key : map.keySet()) {
     System.out.println(key); // prints the key
     System.out.println( map.get(key) ); // prints the value
}

You can also use entrySet() instead of keySet() or values() in order to iterate through the key->value pairs.

平定天下 2024-10-17 18:01:55

TreeMap 可能是您能找到的最接近的东西。

您可以通过调用 TreeMap.keySet(); 并迭代返回的 Set 来迭代键:

// assume a TreeMap<String, String> called treeMap
for(String key : treeMap.keySet())
{
    string value = treeMap[key];
}

这相当于:

// assume a SortedDictionary called sortedDictionary
foreach(var key in sortedDictionary.Keys)
{
    var value = sortedDictionary[key];
}


You could also try the following:

// assume TreeMap<String, String> called treeMap
for (Map.Entry<String, String> entry : treeMap.entrySet())
{
    String key = entry.getKey();
    String value = entry.getValue();
}

这相当于以下 .NET 代码:

// assume SortedDictionary<string, string> called sortedDictionary
foreach(KeyValuePair<string, string> kvp in sortedDictionary)
{
    var key = kvp.Key;
    var value = kvp.Value;
}

TreeMap is probably the closest thing you're going to find.

You can iterate over the keys by calling TreeMap.keySet(); and iterating over the Set that is returned:

// assume a TreeMap<String, String> called treeMap
for(String key : treeMap.keySet())
{
    string value = treeMap[key];
}

It would be the equivalent of:

// assume a SortedDictionary called sortedDictionary
foreach(var key in sortedDictionary.Keys)
{
    var value = sortedDictionary[key];
}


You could also try the following:

// assume TreeMap<String, String> called treeMap
for (Map.Entry<String, String> entry : treeMap.entrySet())
{
    String key = entry.getKey();
    String value = entry.getValue();
}

Which is the equivalent to the following .NET code:

// assume SortedDictionary<string, string> called sortedDictionary
foreach(KeyValuePair<string, string> kvp in sortedDictionary)
{
    var key = kvp.Key;
    var value = kvp.Value;
}
趴在窗边数星星i 2024-10-17 18:01:55

您需要的是 entrySet() SortedMap(TreeMap)的方法。

What you need is entrySet() method of SortedMap (TreeMap).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文