Considering the following example:
class Person {
int citizenNumber; // key
int age;
string name;
constructor(citizenNumber, age, name);
}
Map<int, Person> personsMap = {};
//Map allows the inconsistency fault: key != citizenNumber (object key)
personsMap.put(1, new Person(2, 18, "John"));
// Set would allow this if search by a key was possible
Set<Person> personsSet = { new Person(1, 20, "Paul") };
int searchableCitizenNumber = 1;
// Person should implement/extend some key like `key => citizenNumber`
Person person = personsSet.get(searchableCitizenNumber);
Is there any data strucuture that:
- Does not allow duplicates
- Allow queries from a given key stored in the object itself