Skip to content

Instantly share code, notes, and snippets.

@yongjun823
Created August 19, 2017 08:46
Show Gist options
  • Select an option

  • Save yongjun823/d0bc70871faf990e3f40740442f519e0 to your computer and use it in GitHub Desktop.

Select an option

Save yongjun823/d0bc70871faf990e3f40740442f519e0 to your computer and use it in GitHub Desktop.
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
/**
* Created by LikeJust on 2017-08-19.
* TODO: Question
*/
public class HuWa {
static public void main(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, CloneNotSupportedException {
System.out.println();
MyMap<MeMe, String> meMeStringMyMap = new MyMap<>();
MeMe m1 = new MeMe(10, meMeStringMyMap);
meMeStringMyMap.put(m1, "Hello");
System.out.println(m1);
System.out.println(meMeStringMyMap);
m1 = m1.setV(10);
System.out.println(m1);
System.out.println(meMeStringMyMap);
}
}
class MyMap<K, V> extends HashMap<K, V> {
K newPut(MeMe meMe) throws InvocationTargetException, IllegalAccessException, NoSuchMethodException, CloneNotSupportedException {
if (containsKey(meMe)) {
V v = get(meMe);
remove(meMe);
K newk = (K) meMe.clone();
put(newk, v);
return newk;
} else {
return null;
}
}
}
class MeMe implements Cloneable {
private int v;
private MyMap<MeMe, String> myMap = null;
@Override
protected Object clone() throws CloneNotSupportedException {
MeMe me = (MeMe) super.clone();
me.myMap = myMap;
return me;
}
MeMe(int v, MyMap<MeMe, String> myMap) {
this.v = v;
this.myMap = myMap;
}
MeMe setV(int v) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, CloneNotSupportedException {
this.v = v;
return myMap.newPut(this);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment