Getting keys from a SortedMap

  • Thread starter nooneinparticular314159
  • Start date
N

nooneinparticular314159

Hello. I am storing some data in a TreeMap, and I need to get a subset
of that data. Conveniently, I can get exactly the data I want using
tailmap, which returns a SortedMap. Not so conveniently, I need to get
my objects out of the SortedMap, which seems to require knowing what
the keys are that are used to index the SortedMap. I don't necessarily
know what these keys are. Nothing that I've tried has given me the
actual keys. Is there a way to get the keys out of a SortedMap?
Alternately, is there a way to iterate over the elements in a
SortedMap? I don't think that I can get the first or last element and
remove it, because that will destroy the object backing the map.

Thanks,
Michael
 
O

Owen Jacobson

Hello. I am storing some data in a TreeMap, and I need to get a subset
of that data. Conveniently, I can get exactly the data I want using
tailmap, which returns a SortedMap. Not so conveniently, I need to get
my objects out of the SortedMap, which seems to require knowing what
the keys are that are used to index the SortedMap. I don't necessarily
know what these keys are. Nothing that I've tried has given me the
actual keys. Is there a way to get the keys out of a SortedMap?
Alternately, is there a way to iterate over the elements in a
SortedMap?

Something wrong with using keySet (), which returns a set of all keys on
the map, or entrySet (), which returns a set of all key->value mappings on
the map? They're inherited from Map to SortedMap.
 
T

Thomas Hawtin

Something wrong with using keySet (), which returns a set of all keys on
the map, or entrySet (), which returns a set of all key->value mappings on
the map? They're inherited from Map to SortedMap.

Or if you just want the values, use Map.values.

Tom Hawtin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top