I am using HashSet which has some attributes used for all elements and then adding this Hashset to a HashMap corresponding for each element. Additionally few attributes are added for specific elements( ex THEAD ).
But the later added attribute align is present for both Table and THEAD. Is there something wrong in the below code.
private static HashMap<String, Set<String>> ELEMENT_ATTRIBUTE_MAP =
new HashMap<String, Set<String>>();
HashSet<String> tableSet =
new HashSet<String>(Arrays.asList(new String[]
{HTMLAttributeName.style.toString(),
HTMLAttributeName.color.toString(),
HTMLAttributeName.dir.toString(),
HTMLAttributeName.bgColor.toString()}));
ELEMENT_ATTRIBUTE_MAP.put(HTMLElementName.TABLE, new HashSet<String>(tableSet));
// Add align only for Head
tableSet.add(HTMLAttributeName.align.toString());
ELEMENT_ATTRIBUTE_MAP.put(HTMLElementName.THEAD, tableSet);
Your code should work as you expect. Consider the following (simplified) example which shows the behaviour:
Note that Java variables are references to objects, not objects themselves, so when you call
map.put("key", strings)it is a reference to the underlyingHashSetthat is passed; hence when you later update theHashSettheHashMapis updated too.