0

I defined a hashmap as follows

HashMap<String, List<String>> hashmap = new HashMap<String, List<String>>(); 

I can get its content out by doing

Set<Map.Entry<String, List<String>>> keys = hashmap.entrySet(); for (Map.Entry<String,List<String>> entry : hashmap.entrySet()) { String key = entry.getKey(); List<String> thing = entry.getValue(); System.out.println (key); System.out.println (thing); } 

However, I would like to know:

  • How could I retrieve its content to an ordinary string?
  • Is it possible to access the strings on the fly? (without doing (1)) I mean, the same way you do string[0], etc
  • Where is the length of the list stored?
6
  • 1
    1) Which info are you referring to? ... 2) You mean like using a string array? ... 3) hashmap.size()? Or do you want to have the length of List<String>? Commented Feb 18, 2016 at 15:00
  • ad 2): A HashMap is not ordered, so it makes no sense to access it by index. Commented Feb 18, 2016 at 15:01
  • 1
    It's unclear what you're asking. You can do map.toString(), and you'll get a textual representation of the whole map, if that's what you want. Your second question doesn't make sense to me. A Map is not an array, and you can't access its elements as if it were one. The length of the list is stored in the list. You can access it by calling size() on the list, as on every other collection. The javadoc is your friend. Commented Feb 18, 2016 at 15:01
  • 2
    HashMap.entrySet().toArray(); returns all Entries as an Array. Commented Feb 18, 2016 at 15:02
  • 1
    Welcome to Stack Overflow! It is preferred if you can post separate questions instead of combining your questions into one. That way, it helps the people answering your question and also others hunting for at least one of your questions. Thanks! Commented Feb 18, 2016 at 15:10

2 Answers 2

2

Assuming that String key = "str"; exists in the map. You can:

int mapSize = hashmap.size(); // get the map's size List<String> list = hashmap.get("str"); // get a list for a key String first = hashmap.get("str").get(0); // get a string in a list int listSize = hashmap.get("str").size(); // get the size of a list char ch = hashmap.get("str").get(0).charAt(0); // get a char of a string in a list in the map 
Sign up to request clarification or add additional context in comments.

4 Comments

The thing is that I don't have to know what are the keys named
@Peter then you can get the set of key using the keySet() method. Then loop over the set and determine what you want about the lists of strings.
How could that be done? I don't have much experience with maps
@Peter see my answer
1

Instead of manually getting the set of keys, how about using the keySet() method on the HashMap object from Java?

Use of keySet() looks like:

Set<String> keys = hashmap.keySet(); 

For the third bullet, see the size() method.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.