One of the most powerful uses of tuples is for creating keys on-the-fly. Tuples can be used in hash maps to store and retrieve objects by a multi-valued key.

Map<Pair<String, String>, Integer> population =
new HashMap<Pair<String, String>, Integer>(); population.put( Tuple.from("TX", "Dallas"), 1213825); population.put( Tuple.from("TX", "Fort Worth"), 624067); population.put( Tuple.from("IL", "Springfield"), 203564); population.put( Tuple.from("NM", "Albuquerque"), 494236); int p = population.get( Tuple.from("TX", "Fort Worth")); // p = 624067

The tuples hash value is based on the hash value of its elements. It is also sensitive to the order of the elements.

Tuples can also be used as keys in sorted collections. They are sorted by the first element in increasing order, and then by the next element, and so on.

SortedSet<Pair<Character,Integer>> bingo =
    new TreeSet<Pair<Character,Integer>>();
bingo.add(Tuple.from('B',5));
bingo.add(Tuple.from('N', 41));
bingo.add(Tuple.from('I', 23));
bingo.add(Tuple.from('G',55));
bingo.add(Tuple.from('O', 69));
bingo.add(Tuple.from('I', 27));
bingo.add(Tuple.from('O', 61));

String s = bingo.toString();
// s = "[(B, 5), (G, 55), (I, 23), (I, 27), "
//     "(N, 41), (O, 61), (O, 69)]"