79

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:

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.

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?

idungotnosn
  • 2,001
  • 4
  • 29
  • 36
  • The way your `addVerb()` method checks for the existence of the value first, shouldn't the verb be the key? – Marcelo Jul 05 '11 at 22:55
  • 2
    This is probably the simplest question I've asked on SO, yet it has gotten me by far the most upvotes. – idungotnosn Aug 16 '19 at 16:35

6 Answers6

110

Use the keySet() method to return a set with all the keys of a Map.

If you want to keep your Map ordered you can use a TreeMap.

Marcelo
  • 11,218
  • 1
  • 37
  • 51
  • 1
    How can I get a List of keys and not a Set ? Because I need to access the individual keys after the Map is made. – gonephishing Jun 17 '15 at 09:59
  • 8
    @gonephishing You can use the set as a parameter for the constructor of an ArrayList for example.. – Marcelo Jun 24 '15 at 18:51
104

Using map.keySet(), you can get a set of keys. Then convert this set into List by:

List<String> l = new ArrayList<String>(map.keySet());

And then use l.get(int) method to access keys.

PS:- source- Most concise way to convert a Set<String> to a List<String>

Community
  • 1
  • 1
Anu
  • 1,041
  • 1
  • 7
  • 2
14
List<String> yourList = new ArrayList<>(map.keySet());

This is will do just fine.

rajat meena
  • 353
  • 4
  • 13
9

Since Java 8:

List<String> myList = map.keySet().stream().collect(Collectors.toList());
Cengiz
  • 5,375
  • 6
  • 52
  • 77
9
map.keySet()

will return you all the keys. If you want the keys to be sorted, you might consider a TreeMap

Kal
  • 24,724
  • 7
  • 65
  • 65
-1
 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

Zeeshan Mehdi
  • 1,241
  • 1
  • 17
  • 18