What is wrong with this design? Overriding or overloading java.util.HashMap

This question was asked me in an interview with MS. I want to know the exact design problem in this piece of code. The code has already been provided to find a design problem.

I have a MyHashMap class that extends the Java HashMap class. In the MyHashMap class, I have to store some information about employees. The key of this card will be firstName + lastName + Address.

public MyHashMap extends HashMap<Object, Object> {
  //some member variables 
  //
  public void put(String firstName, String lastName, String Address, Object obj) {
       String key =   firstName + lastName+ Address;
       put(key, obj);
  }

  public Object get(String firstName, String lastName, String Address) {
       String key =   firstName + lastName+ Address;
       return get(key);
  }

  public void remove(Strig key) {
        put(key, ""); 
  }

  //some more methods 
}

What is wrong with this design? Should I subclass HashMap or should I declare HashMap as a member variable of this class? Or should I use hashCode / equals methods?

+5
source share
7 answers

, , , String . , :

final MyHashMap map = new MyHashMap();

map.put("foo", "", "baz", new Object());
map.put("", "foo", "baz", new Object()); // Overwrites the previous call

, , , Object, String , . , keySet Map, Set String, , - Map Integer, .

, . MyHashMap Map put, get remove, . , , , .

, , , :

Map<Object, Object> map = new MyHashMap();

none . , firstName, lastName address, , equals hashCode, HashMap.

+13

, , HashMap<Oject, Object>, . "" Map, - , Map (- ) .

- EmployeeData hashCode() equals() (, , ). Map - HashMap<EmployeeData, Object>. , , Object.

+5

HashMap - .

, " " " Java", , , API .

, , .

+4

:

public MyHashMap extends HashMap<Oject, Object>

. .

, , ,

public MyHashMap extends HashMap<NameAddress, Employee>

. Address , equals hashCode.

, Set , , HashMap . , :

public EmployeeSet implements Set<Employee> {
  private static class NameAddress {
    public boolean equals();
    public int hashCode();
  } 

  private HashMap employees = new HashMap<NameAddress, Employee>();   

  public add(Employee emp) {
    NameAddress nad = new NameAddress(emp);
    empoyees.add(nad, emp);
  }

  public remove(Employee emp) {
  }
}

.

EDIT , NameAddress , . hashCode .

+3

, , . , remove (1) remove(String firstName, String lastName, String Address) (2), ( ), . :

public void remove(String firstName, String lastName, String address) {
   String key =   firstName + lastName + address;
   return remove(key);
}
+3

" " , remove:

  • Map.remove(<K>) , ( )

  • , , !

, , , . ( null ...)

, , . ( , Map MyHashMap, ....)

+2

I think it depends a lot on the context and what they exactly asked. For example, this is a very parallel context, you should use a hashtable instead. In addition, you must specify types (String, Object) instead of (Object, Object) in order to get compiler support on the keys.

I think it would be better to implement the Map interface and save the HashMap / HashTable as an internal parameter of the object.

0
source

All Articles