开发者

Extend HashMap to add putChildren method

I have question, I want to develop a programme about extend the hashmap to add putchildren method.. I wrote main class, but now I want to write putChildrenValue method.. My question is : I need to implement a putChildrenValue method with 3 parameters, String key, String key, ObjectValue. It will store the system as described above accordingly.

When I finished this method When you finish the method data

Key1 = "RUBY" value=HashMap which has -> "key2" = 5248 && "VALUE" = German
Key1 = "PYTHON" value=HashMap which has -> "key2" = 1234 && -> "VALUE" = German

My main class is :

public static void main(String [] args)
{
    ExtendedHashMap extendedMap = new ExtendedHa开发者_如何学编程shMap();
    extendedMap.put (“Row1”, “Column1”, “German”);
    extendedMap.put (“Row1”, “Column2”, “English”);
    extendedMap.put (“Row1”, “Column3”, “Spanish”);
    extendedMap.put (“Row2”, “Column1”, “Ruby”);
    extendedMap.put (“Row2”, “Column2”, “Python”);
    extendedMap.put (“Row3”, “Column3”, “Java”);
}

Can anyone help me?


It could go like this:

import java.util.HashMap;

public class ExtHashMap<K1, K2, V> extends HashMap<K1, HashMap<K2, V>> {

  public ExtHashMap() {
    super();
  }

  public void putChildrenValue(K1 key1, K2 key2, V value) {

    HashMap<K2, V> childMap = get(key2);
      if (childMap == null) { 
        childMap = new HashMap<K2, V>();
        put(key1, childMap);
      }
      childMap.put(key2, value);
    }
  }
}

The following snippet gives an example on how to create and use it:

ExtHashMap<String, int, String> map = new ExtHashMap<String, int, String>();
map.putChildValue("RUBY", 1234, "VALUE1");
map.putChildValue("PYTHON", 4321, "VALUE2");

The childmaps are autocreated on demand.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜