2

Before I reinvent the wheel does java already have this data structure: Map<String, Map<String, String>> as a single type?

I need to be able to call .get(key) and it return a Map<String, String>.

Or is there a common library that has it?

Cheetah
  • 13,785
  • 31
  • 106
  • 190
  • 6
    `Map>` is not very complicated. Just use it. BTW: This *is* a "single type". –  Nov 05 '14 at 15:10
  • possible duplicate of [Map of Maps data structure](http://stackoverflow.com/questions/3093718/map-of-maps-data-structure) – Andrés Oviedo Nov 05 '14 at 15:11
  • lol theoritically you can have Map>> if you wanted to. heheh – nafas Nov 05 '14 at 15:12
  • 2
    Another alternative is use a Map, String> and roll your own Pair or use one from for instance Apache Commons. – aioobe Nov 05 '14 at 15:13
  • 1
    Another alternative is Guava's *Table*: http://stackoverflow.com/questions/14645525/map-in-map-in-guava But @Tichodroma is right. You can just use exactly what you described – vefthym Nov 05 '14 at 15:15
  • 1
    I would agree with @vefthym, check out ImmutableTable or Table from Guava, this should suit your needs. – Andrew Landsverk Nov 05 '14 at 15:20

1 Answers1

0

To sum up what people have written in the comments (if I may), you have plenty of options here:

  • Use exactly what you described, i.e., a Map<String,Map<String,String>>, as Tichodroma suggests (after all it's not so complicated).
  • Use a Map<Pair<String,String>, String> where Pair is either a class that you defined, or one of Apache Common's Pair classes, like this one, as aioobe suggest. Keep in mind, however, that there is a reason why Java doesn't have a Pair class.
  • My advice would be to use Guava's Table<String,String,String>, or ImmutableTable (as Quantas adds), which do exactly what you want. See this post for more details.

Plase, do not upvote/accept this answer, unless you picked the last option.

Community
  • 1
  • 1
vefthym
  • 7,422
  • 6
  • 32
  • 58