0

I know I have the option to make a POJO object to store this. However, I am trying to do it in a nested HashMap. Whenever I do the following I am getting Type Safety Warnings.

Map<String, Map<String, String>> map = new HashMap<String, Map<String, String>>();
map.put("fund_cde", new HashMap(){{put("ABC", "ABC is not valid message.");}});

If I set the nested HashMaps type like so:

map.put("fund_cde", new HashMap<String, String>(){{put("ABC", "ABC is not valid message");}});

I end up with this warning instead:

The serializable class does not declare a static final serialVersionUID field of type long

Any advice on correct way to do this? I had a look at How to put/get values into/from Nested HashMap but I'm still not understanding it.

Thanks!

Community
  • 1
  • 1
Taobitz
  • 546
  • 3
  • 10
  • 22
  • 1
    Just parameterize your `HashMap` in your first example. And add elements on multiple lines. These anonymous classes are messy and not easily readable. – Sotirios Delimanolis Feb 19 '14 at 22:58
  • 1
    The second is correct. Serializability is a separate issue. – keshlam Feb 19 '14 at 22:59
  • 1
    ^^ There is nothing wrong with the code snippet you have posted (the second version). The error you are now receiving is coming from code you aren't showing us (writing a simple two line test of the code you show would tell you that). – Brian Roach Feb 19 '14 at 23:01
  • Thanks - Surely I'm not going to add serial ID for each. (Don't know enough about serializability though.) Will take a look into it again. Least my hashmap is right. – Taobitz Feb 19 '14 at 23:09
  • Just make all the maps and cast as required on retrieval. – Hot Licks Feb 19 '14 at 23:55

2 Answers2

0

There is another way to achieve this if you like you can use because I also tried in different manner that also give warning.You can use this

Map<String,String> map1=new HashMap<String,String>();
    map1.put("ABC", "XYZ");
    Map<String ,Object> map=new HashMap<String,Object>();
    map.put("axz", map1);
Sheel
  • 847
  • 2
  • 8
  • 20
-1

If you really want to do away with the warning. Either include a version id like:

map.put("fund_cde", new HashMap(){
        private static final long serialVersionUID = 1L;
        {put("ABC", "ABC is not valid message.");}
});

or add the @SuppressWarnings("serial") annotation to the method. If you use an IDE like eclipse it can offer you these options as auto fixes.

user1945457
  • 304
  • 1
  • 3
  • 7
  • Um, this isn't the issue. `Hashmap` already implements `Serializable` and has a `serialVersionUID` - http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/util/HashMap.java#1023 – Brian Roach Feb 19 '14 at 23:13
  • Yes HashMap does but the Anonymous Class does not. You can confirm this by trying to get: new HashMap() {{System.out.println(serialVersionUID);}} to compile. – user1945457 Feb 20 '14 at 08:51
  • And in contrast: new HashMap() {private static final long serialVersionUID = 1L; {System.out.println(serialVersionUID);}} compiles just fine. – user1945457 Feb 20 '14 at 08:57
  • Also Bitzal states in the question that the warning is "The serializable class does not declare a static final serialVersionUID field of type long" – user1945457 Feb 20 '14 at 08:58