I’ve created the following structure which maps unique double values to one or more pairs of integers:
@SuppressWarnings('boxing') private static final HashMap<Double, Integer[][]> rules = new HashMap<Double, Integer[][]>() { private static final long serialVersionUID = 1L; { put(-0.6, new Integer[][] { { 1, 3 } }); put(-0.3, new Integer[][] { { 2, 2 } }); put(0.0, new Integer[][] { { 2, 4 }, { 3, 3 }, { 4, 2 } }); put(0.3, new Integer[][] { { 4, 4 } }); put(0.6, new Integer[][] { { 5, 3 } }); } };
Can I rewrite this so it’s simpler – i.e not have to deal with warnings (serialVersionUID, boxing), and it being so verbose?
Using a class for the pairs of integers should be the first. Or is this a coincidence, that all arrays containing a bunch of pairs?
The second thing is, that these initialization-data could be read from a configuration-file.
Edit: As I looked again on this code, I realized that Doubles as keys in a Map is somewhat risky. If you produce Doubles as a result of an mathematical operation, it is not clear, if they will be equal for the computer (even if they are equal in a mathematical sense). Floating-point-numbers are represented as approximation in computers. Most likely you want to associate the values with the interval (example 0.0-0.3) and not the value itself. You may avoid trouble, if you always use the same constants as keys in the array. But in this case you could use an enum as well, and no new programmer runs into trouble, if he uses his calculated doubles as keys in the map.