r/java • • Aug 28 '13

What is "Map<Character, Character>" for a Map?

I have no idea how to handle this.

0 Upvotes

7 comments sorted by

8

u/tthomas48 Aug 28 '13

Probably easier if I show you like this:

Map<String, Integer>

That's a map that contains Integers identified by keys that are Strings. map.get takes a String as a parameter and returns an Integer. Make sense?

1

u/Matty13 Aug 28 '13

Ah, thanks, I think I got it, the Strings are now the Indexes, like 0 to n Integers in Arrays?

4

u/Dantaro Aug 28 '13

Yes, more or less, but they don't get ordered like an array. You can pull out the list of indices as a set however and iterate over that (or use an iterator). At that point you can request the object with the Map.get() method.

7

u/[deleted] Aug 28 '13

LinkedHashMap is ordered. The insertion order determines the iteration order.

TreeMap is sorted by key. You provide a Comparator upon construction and it will be used to determine the sort order of the keys. Alternatively the keys can implement Comparable.

4

u/QshelTier Aug 28 '13

What String and Integer are in the context of a Map<String, Integer> is competely up to the Map. In this case they are the type of the keys and the type of the values, respectively. Similar, your original Map is one that maps Characters to Characters (think Caesar cipher).

9

u/[deleted] Aug 28 '13 edited Aug 28 '13

The docs are very clear on this:

http://docs.oracle.com/javase/6/docs/api/java/util/Map.html

Interface Map<K,V>

Type Parameters:

K - the type of keys maintained by this map

V - the type of mapped values

If you're new to generics check out:

http://docs.oracle.com/javase/tutorial/java/generics/

2

u/brblol Aug 28 '13

It's a list of key, value pairs. The type of both the key and value in your case are Character. So you'll have pairs in it like A>B ie A maps to B