Duncan Palmer
Duncan Palmer

Reputation: 2913

Getting specific values in a TreeMap

So, I have grabbed what I believe Treemap as a String which looks like this:

{Username1={password=password1}, Username2={password=password2}}

How would I go about getting the values 'Username' and 'Username2' As well as Username1's Password(password1) and Username2's Password(password2)? Is there a way to iterate over those values in an array or something like that?

Any help will be appreciated.

Thanks.

Upvotes: 0

Views: 216

Answers (1)

assylias
assylias

Reputation: 328923

TreeMap is an ordered Map. All maps have those 3 methods:

map.keySet(); //returns a Set containing the keys (Username in your case)
map.values(); //returns a Collection containing the values (the passwords in your case)
map.entrySet(); //retrurns a Set of entries (an entry is a key + value)

If you want to access both in a loop, the best way is through the entryset:

for (Map.Entry<UserName, Password> e : map.entrySet()) {
    UserName user = e.getKey();
    Password pwd = e.getValue();
}

Upvotes: 3

Related Questions