Java 如何从哈希映射返回键列表?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6589744/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to return a list of keys from a Hash Map?
提问by idungotnosn
I'm currently trying to make a program that conjugates verbs into Spanish. I've created a Hash Table that contains a key and an instantiation of the object Verb. The key is a string that has the infinitive form of the verb (for example, "hablar"). This is the code that I have so far for the hash map:
我目前正在尝试制作一个将动词结合成西班牙语的程序。我创建了一个哈希表,其中包含一个键和对象 Verb 的实例化。键是具有动词不定式形式的字符串(例如,“hablar”)。这是我迄今为止的哈希映射代码:
public class VerbHashMap {
HashMap<String, Verb> verbHashMap;
public VerbHashMap(){
verbHashMap = new HashMap();
}
}
Each verb's key in the HashMap is based on the infinitive form of the verb. For example, the string "hablar" is the key for a Spanish verb. The class Verb has a method called getInfinitive() which returns a string that contains the infinitive form of the verb.
HashMap 中每个动词的键都基于动词的不定式形式。例如,字符串“hablar”是西班牙语动词的键。Verb 类有一个名为 getInfinitive() 的方法,它返回一个包含动词不定式形式的字符串。
public boolean addVerb(Verb verb){
if(verbHashMap.containsValue(verb.getInfinitive()){
return false;
}
else{
verbHashMap.put(verb.getInfinitive(), verb);
return true;
}
}
The question is what is the most efficient way to create a method that returns a list of all the verbs in the Hash Map in alphabetical order? Should I have the method return an ArrayList which includes the keys of all the objects in the Hash Map? Or is there a much more efficient way to go about this?
问题是创建一个按字母顺序返回哈希映射中所有动词列表的方法的最有效方法是什么?我应该让该方法返回一个包含哈希映射中所有对象键的 ArrayList 吗?或者有没有更有效的方法来解决这个问题?
采纳答案by Marcelo
回答by Kal
map.keySet()
will return you all the keys. If you want the keys to be sorted, you might consider a TreeMap
将归还您所有的钥匙。如果您希望对键进行排序,则可以考虑使用 TreeMap
回答by Anu
Using map.keySet()
, you can get a set of keys. Then convert this set into List
by:
使用map.keySet()
,您可以获得一组密钥。然后将此集合转换List
为:
List<String> l = new ArrayList<String>(map.keySet());
And then use l.get(int)
method to access keys.
然后使用l.get(int)
方法访问密钥。
PS:- source- Most concise way to convert a Set<String> to a List<String>
PS:- source-将 Set<String> 转换为 List<String> 的最简洁方法
回答by Zeeshan Mehdi
for(int i=0;i<ytFiles.size();i++){
int key = ytFiles.keyAt(i);
Log.e("key", String.valueOf(key));
String format = ytFiles.get(key).getFormat().toString();
String url = ytFiles.get(key).getUrl();
Log.e("url",url);
}
you can get key by method keyat and you have to pass the index then it will return key at that particular index. this loop will get all the key
您可以通过 keyat 方法获取键,并且必须传递索引,然后它将返回该特定索引处的键。这个循环会得到所有的钥匙
回答by Cengiz
Since Java 8:
从 Java 8 开始:
List<String> myList = map.keySet().stream().collect(Collectors.toList());
回答by rajat meena
List<String> yourList = new ArrayList<>(map.keySet());
This is will do just fine.
这是会做的很好。