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.

1
  • it seems like you missed ) it somewhere... just check braces carefully. Commented Apr 29, 2015 at 14:14

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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);}});
}};

Comments

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();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.