1

I'm trying to control some permissions on my App. Yesterday I learn how to created Double Brace Initialization, It helped a lot. But now I'm trying to use it nested, but I'm getting a

')' expected

from the IDE (Android Studio)

Here is my code:

public static final Map<String, List> ALL_PERMISSIONS = new HashMap<String, List>() {{
    put("Change-maps", new ArrayList<Integer>(){{add(R.id.button_change_view);}};);
    put("Stores-info-view", new ArrayList<Integer>(){{add(R.id.details_fragment);}};);
    put("Competitors-layer", new ArrayList<Integer>(){{add(R.id.switch_concorrentes);}};);
}};

am I missing something in it? is that a bad approach?

PS: I'm trying this approach because in the future I'll use some keys with more than one View (Integer), and some keys with a list of String.

wviana
  • 1,619
  • 2
  • 19
  • 47

3 Answers3

3

You should format/indent your code (Ctrl-Shift-F by default in Eclipse).

You'd see that your anonymous ArrayList class declaration (outside set of curly brackets) cannot be followed by a semi-colon.

Here's a formatted example that will work:

public static final Map<String, List> ALL_PERMISSIONS = new HashMap<String, List>() {
    {
        put("Change-maps", new ArrayList<Integer>() {
            {
                add(R.id.button_change_view);
            }
        });
        put("Stores-info-view", new ArrayList<Integer>() {
            {
                add(R.id.details_fragment);
            }
        });
        put("Competitors-layer", new ArrayList<Integer>() {
            {
                add(R.id.switch_concorrentes);
            }
        });
    }
};

Note

Also mind the raw types or suppress the warnings.

Mena
  • 47,782
  • 11
  • 87
  • 106
2

If you look at this code:

Map<String, String> map = new HashMap<String, String>();
map.put( "string1", "string2" );

You can notice that the objects you are passing in parameters are not followed by a ;.

In your case, the second object you are passing is this one:

new ArrayList<Integer>(){{add(R.id.button_change_view);}}

So, you don't need the ; before your put's closing parenthesis, like this :

public static final Map<String, List> ALL_PERMISSIONS = new HashMap<String, List>() {{
        put("Change-maps", new ArrayList<Integer>(){{add(R.id.button_change_view);}});
        put("Stores-info-view", new ArrayList<Integer>(){{add(R.id.details_fragment);}});
        put("Competitors-layer", new ArrayList<Integer>(){{add(R.id.switch_concorrentes);}});
}};
G.T.
  • 1,557
  • 1
  • 12
  • 24
1

I would not encourage the use of double brace initilization. As this answer explains, it may

  1. surprises your colleagues and is hard to read
  2. harms performance
  3. may cause problems with object equality (each object created has a unique class object).

I would suggest, if possible, to use Guava ImmutableMap and ImmutableList

for example:

public static final Map<String, List> ALL_PERMISSIONS =  ImmutableMap.<String, List>of(
        "Change-maps", ImmutableList.of(R.id.button_change_view),
        "Stores-info-view", ImmutableList.of(R.id.details_fragment),
        "Competitors-layer", ImmutableList.of(R.id.switch_concorrentes)
);

or if you need to add more elements:

public static final Map<String, List> ALL_PERMISSIONS =  new ImmutableMap.Builder<String, List>()
        .put("Change-maps", ImmutableList.of(R.id.button_change_view))
        .put("Stores-info-view", ImmutableList.of(R.id.details_fragment))
        .put("Competitors-layer", ImmutableList.of(R.id.switch_concorrentes))
        //(... and so on...
        .build();
Community
  • 1
  • 1
Xavier Delamotte
  • 3,519
  • 19
  • 30