Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I have the following "working" static map:

private static final Map<String, Object[]> S_MAP;
static {
Map<String, Object[]> map = new HashMap<String, Object[]>();
map.put("somekey", new Object[] {"SSS", new Integer("6")});
map.put("bad", new Object[] {new Integer("6"), "VVV"});// s/b same as above
S_MAP = Collections.unmodifiableMap(map); }

The Object[] I dislike. I have looked at inner class, but wondering how others would handle it. Inner class instantiation seems "odd".

So how would I change it so that String and Integer are compile time checked, since with Object[], anything goes.

The object is simply a string and integer. An inner class would require new Outerclass.new Innerclass("somestring",someint) additional, inner class would just be holding some values

public class SnI {
public String s;
public Integer i;
public SnI(....){
...}
}

Seemed overkill. But I suppose better than having an Object[] with casts.

share|improve this question

closed as unclear what you're asking by Simon André Forsberg, Malachi, BeetDemGuise, syb0rg, 200_success Jul 2 '14 at 22:20

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

6  
Can you add some additional information, what are the String, Integer used for? Can you please add some real context for us to understand better? As it currently stands your question does not have very high quality and is on the edge of being off-topic, but I would really like to help you. –  Simon André Forsberg Jul 2 '14 at 18:23
1  
Also please explain, and preferably show, your inner class instantiation that you think is "odd". –  Simon André Forsberg Jul 2 '14 at 18:43
2  
@Vogel612 No, please not. Using Pair is an anti-pattern. If the two elements belong together semantically, write a class and give it a proper name. Pair is too generic. –  Ingo Bürk Jul 2 '14 at 18:52
2  
@Paul What does this map hold? What are the semantics, what are possible values? What is the context it is being used in? –  Ingo Bürk Jul 2 '14 at 18:53
1  
@Vogel612 Well, that's part of what classes are for ;) Even writing classes that simply wrap a single element may be useful in order to avoid things like string-typing. –  Ingo Bürk Jul 2 '14 at 18:57

1 Answer 1

  1. You could change

    new Object[] { "SSS", new Integer("6") }
    

    to

    new Object[] { "SSS", 6 }
    

    It's the same.

  2. Please note that although the map is immutable the underlying arrays can be changed. Consider this:

    S_MAP.get("somekey")[1] = 7;
    
  3. If you have just a few entries in the map you can try ImmutableMap from Guava. It has a few of builder methods, like

    • of(K k1, V v1)
    • of(K k1, V v1, K k2, V v2)
    • of(K k1, V v1, K k2, V v2, K k3, V v3)
    • etc.

    So you could have something like this:

     ImmutableMap<String, Object[]> a = ImmutableMap
            .of("somekey", new Object[] {"a", 6},
                "bad", new Object[] { 6, "VVV"}  
             );
    

    If you have more keys you can use its builder:

    ImmutableMap<String, Object[]> b = ImmutableMap.<String, Object[]> builder()
        .put("somekey", new Object[] {"a", "b"})
        .put("bad", new Object[] { new Integer("6"), "VVV"})
        .build();  
    

    (Thanks to Ingo Bürk for the builder() hint.)

  4. Another option is a Multimap (also from Guava):

    ImmutableMultimap<String, Object> map = ImmutableListMultimap.<String, Object> builder()
            .putAll("somekey", "SSS", 6)
            .putAll("bad", 6, "VVV")
            .build();
    

    A big advantage here is that the values are also immutable.

share|improve this answer
1  
Even if it's more elements, Guava comes with builder() methods, eliminating the need for static blocks. :) –  Ingo Bürk Jul 2 '14 at 18:57
    
@IngoBürk: Oh, cool, thanks! –  palacsint Jul 2 '14 at 18:58

Not the answer you're looking for? Browse other questions tagged or ask your own question.