I'm wondering if there are any Java data structures or libraries that would allow me to have multiple keys while not having duplicate keys?
In my use case I have two different types of keys: Personal Identification Number and Driver License. I want to use either keys to look up a value (their car in this example). When I try to add a duplicate key (pin-1), there should be an error as pin-1 is already assigned.
structure.add("pin-1", "driverLicense-1", Car.TOYOTA);
structure.add("pin-2", "driverLicense-2", Car.FORD);
structure.add("pin-1", "driverLicense-3", CAR.FORD); // invalid because pin-1 is already assigned
The only library I have encountered that does something like this is Google’s Guava and its HashBasedTable class implementing Table. The issue I have with this library is that it does not prevent duplicate keys.
Table<String, String, Car> table = HashBasedTable.create();
table.put("pin-1", "driverLicense-1", Car.TOYOTA);
table.put("pin-2", "driverLicense-2", Car.FORD);
table.put("pin-1", "driverLicense-3", Car.FORD); // is valid
HashBasedTabledoes not prevent duplicate keys? The Javadoc says the data is stored in a Map<R, Map<C, V>>. And aMapby definition cannot have duplicate keys.