Alternative to double brace initialization

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-25 05:00:31

问题


I have a nested collection in the form:

HashMap<String, HashMap<String, List<String>>> errorList;

Now I initialize it inline using double braces like this

errorList.put(tempName, new HashMap<String, List<String>>() {{
    put("upl", new ArrayList<String>() {{ add("Y"); add("Upload Success"); }});
}});

This lies in a foreach loop with the value of tempName changing in every iteration. I did this because i couldn't use instances of List<String> or HashMap<String,List<String>> because every time i changed the value in that instance it is reflected in the collection it is nested in. So i am forced to create new instances with double brace initialization.

Thing is: I want to use a list object. Instead of

new ArrayList<String>() {{ add("Y"); add("Upload Success"); }}

I want to use a variable.

How can I do this?


回答1:


Instead of

new ArrayList<String>() {{ add("Y"); add("Upload Success"); }}

you may use

Arrays.asList("Y", "Upload Success")

This gives you a fixed-size list. If you want to be able to add or remove elements later, convert it into an ArrayList:

new ArrayList<>(Arrays.asList("Y", "Upload Success"))

And of course you can put this list into its own variable before putting it into your map structure.

If you want to put either [Y, Upload Success] or [N, Upload Failed] and make sure the lists aren’t shared between map entries, here’s a suggestion: First, outside your loop:

final List<String> successList = Arrays.asList("Y", "Upload Success");
final List<String> failureList = Arrays.asList("N", "Upload Failed");

Then inside your loop

if (wasSuccessful) {
    errorList.put(tempName,
                  Collections.singletonMap("upl", new ArrayList<>(successList)));
} else {
    errorList.put(tempName,
                  Collections.singletonMap("upl", new ArrayList<>(failureList)));
}

You may take the idea one step further and build the maps outside the loop. And again, if you want the inner map to be a HashMap, just convert into one: new HashMap<>(Collections.singletonMap("upl", new ArrayList<>(successList))).

I have not tested the code, I hope you can fix if there is a typo.

You notice I have avoided the double brace initialization completely. While it’s brief, it has an overhead both conceptually and performancewise. You are creating a new anonymous subclass each time, which I don’t find warranted.



来源:https://stackoverflow.com/questions/43891568/alternative-to-double-brace-initialization

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!