Multi-user hash map

I recently had an interview to save a huge amount of employee data in DS.

I gave a solution as a Hashmap with emp Id as the key.

The next question was whether the user wants to search by name, how to implement it. I suggested using emp as the key and keeping all employees with the same name as Arraylist.

The next next question was difficult, you need to create ONE map where the user can search based on the name emp Id or emp. How to implement this on a map?

Implement it in memory in an efficient way.

+4
source share
4 answers

( - , !), , , (, long String). : id name:

Map<?, List<Employee>> map = new HashMap<>();

public void putEmployee(Employee e) {
    map.put(e.id, Arrays.asList(e));    // put by id
    if (!map.containsKey(e.name)) {
        map.put(e.name, new ArrayList<>());
    }
    map.get(e.name).add(e);             // put by name
}

public Employee getById(long id) {
    return map.containsKey(id) ? map.get(id).get(0) : null;
}

public List<Employee> getByName(String name) {
    return map.containsKey(name) ? map.get(name) : Collections.emptyList();
}

.

+1

: - id-as-string.

- 1.

, , , 1 , :

Map<Integer, Employee> employeesById;
Map<String, Set<Employee>> employeesByName;

, , , . , , ID .

+1

- Key, , id:

public enum KeyType {
    ID, NAME;
}

public class SearchKey {
    private KeyType keyType;
    private String value;

    // constructor and getters snipped for brevity sake

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        SearchKey searchKey = (SearchKey) o;

        return keyType == searchKey.keyType && value.equals(searchKey.value);

    }

    @Override
    public int hashCode() {
        int result = keyType.hashCode();
        result = 31 * result + value.hashCode();
        return result;
    }

public class Directory {
    private Map<SearchKey, Set<Employee>> directory = new HashMap<>();

    public void addEmployee(Employee e) {
        // Add search by id
        directory.put
            (new SearchKey(KeyType.ID, e.getId()), Collections.singleton(e));

        // Add search by name
        SearchKey searchByName = new SearchKey(KeyType.NAME, e.getName());
        Set<Employee> employees = directory.get(searchByName);
        if (employees == null) {
            employees = new HashSet<>();
            directory.put(searchByName, employees);
        }
        employees.add(e);
    }

    public Employee getById (String id) {
        // Assume that the ID is unique
        return directory.get(new SearchKey(KeyType.ID, id)).iterator().next();
    }

    public Set<Employee> getByName (String name) {
        return directory.get(new SearchKey(KeyType.NAME, name));
    }
}
0

. , .

1: hashmap emp id emp .

2: emp id, Ex: name = 'XYZ' id = {101,102,103,...}

3. arraylist .

Here we do not store the full detail of the employee twice. Just trying to maintain a relationship between name and id. Thus, it can be memory efficient.

0
source

Source: https://habr.com/ru/post/1608474/


All Articles