Question from the interview. No more data. What should Action.f3() return? Offer another implementation of the Map with the same behavior. For some reason, my method returned null .
Action.class
public class Action { public static Object f3() { Map map = new HashMap(20); map.put("1", "One"); map.put("2", "Two"); map.put("3", "Three"); Map myMap = new MyMap(map); return myMap.get("1"); } } MyMap.class
class MyMap implements Map { private Map anotherMap; public MyMap(Map anotherMap) { this.anotherMap = anotherMap; } public boolean containsKey(Object key) { // ..... } public Object get(Object key) { Iterator it = anotherMap.entrySet().iterator(); while (it.hasNext()) { Map.Entry e = (Map.Entry) it.next(); if (key.equals(e.getValue())) { return e.getKey(); } } return null; } public int size() { //.... } //... }
One. We create our ownmyMapimplementationmyMappassing anotherMapto the constructor, sort of like we need to copy all the fields. And the question is what? And to be honest, some strange methodgetinmaptake an element forO(n)if anymapnot worse thanO(log N)it does ... - pavelgetmethod where is the key and where is the value used? - pavel