I am an experienced programmer but a Java beginner. I have a benchmarking method that accepts a parameter of type Map and performs some tests on it. It can be invoked on a HashMap, Hashtable, IdentityHashMap, TreeMap etc because these all implement Map. They also all implement Cloneable, but Eclipse tells me I am not allowed to invoke the clone() method.
private static double[] timeMapRemoves(Map<String,Integer> map,
Collection<String> data,
int reps) {
Map<String,Integer> map_clone = map.clone(); // OOPS -- "clone not accessible"
So I delve into the Oracle website and I come up with a solution of sorts
Map<String,Integer> map_clone = null;
Method clone = null;
try {
clone = map.getClass().getMethod("clone", null);
map_clone = (Map<String,Integer>)clone.invoke(map, null);
} catch (NoSuchMethodException | SecurityException
| IllegalAccessException | IllegalArgumentException
| InvocationTargetException e) {
e.printStackTrace();
}
I feel that I may, like Drool Rockworm, have delved too deep and missed the canonical solution.