Re: Initializing a Map in an Interface?
Rhino wrote:
Is it possible to do a full-on assignment of specific values to a HashMap
in an interface? If so, how?
Before actually doing this, I recommend reviewing your design to see
whether this really makes sense. A Map seems a bit too much
implementation to belong naturally in an interface. However, if you do
want to do it, you can put a method to generate the map in a static
member class declaration.
import java.awt.Color;
import java.util.HashMap;
import java.util.Map;
public interface ColorMapTest {
Map<String, Color> EIGHT_BIT_COLORS = ColorMapInitializer
.getMap();
static class ColorMapInitializer {
static Map<String, Color> getMap() {
Map<String, Color> result = new HashMap<String, Color>();
result.put("Black", new Color(0, 0, 0));
result.put("Obscure Gray", new Color(51, 51, 51));
result.put("Dark Gray", new Color(102, 102, 102));
result.put("Light Gray", new Color(153, 153, 153));
result.put("Pale Gray", new Color(204, 204, 204));
result.put("White", new Color(255, 255, 255));
return result;
}
}
}