aspose file tools*
The moose likes Java in General and the fly likes Struggling with generics, Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Struggling with generics, "unchecked call to put()"" Watch "Struggling with generics, "unchecked call to put()"" New topic
Author

Struggling with generics, "unchecked call to put()"

Rob Marshall
Ranch Hand

Joined: Aug 17, 2009
Posts: 30
Hi,

This is probably something really simple, but I'm obviously not getting it...

I have the following example:

I've tried every permutation that I can think of, and I can't seem to get rid of:

Any ideas what I'm missing? The code works and I end up with what I want, but I would prefer to fix the warnings (as opposed to simply suppressing them).

Thanks,

Rob
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
You have an evil design going on there...

You will get warnings one or the other way, because you are mixing raw types with parameterized types.

Why not using two HashMaps? One that holds the String, Map combo, and the other the String, List.


JDBCSupport - An easy to use, light-weight JDBC framework -
Rob Marshall
Ranch Hand

Joined: Aug 17, 2009
Posts: 30
Hi Sebastian,

Well, it gets more evil because I got this to work:

You gotta love it

I think that the problem is trying to define things too early. What seems to work best (for me) is to keep the types closer to the actual definitions. In other words, rather than trying to define the whole HashMap up front (ala HashMap<String, HashMap<String, HashMap<String, String>>>, although I may not have tried that one... ), get more specific as I get to where I'm actually defining the maps/lists.

Thanks,

Rob
Rob Marshall
Ranch Hand

Joined: Aug 17, 2009
Posts: 30
Oh, and by the way, the reason this looks so evil is because I'm reading in a configuration file that has multiple types of parameters it defines (it's also inconsistent in it's definitions...I didn't write it) with variable arguments and lists for the different parameters. But I want to keep all of the information in a single map.

Thanks again,

Rob
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
You just shifted away the problem.

Try adding



like you did before, you'll get the same warning.

This design is super type unsafe.
Rob Marshall
Ranch Hand

Joined: Aug 17, 2009
Posts: 30
Sorry, one more thing, all the "new String"'s are superfluous...I guess I went a little overboard
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18981
    
  40



Am I reading this right? You are creating an anonymous inner class that extends the HashMap class, for the purpose so that, you can add an instance initializer, that preloads two key value pairs?

Creating a new class, just so that you can initialize a particular instance is a bit much, isn't it?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18981
    
  40

Rob Marshall wrote:


You gotta love it


No I don't gotta... Creating 5 anonymous inner classes that just initializes one collection is too much. Don't you get annoyed having tons and tons of class files floating around?

Henry
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Oh, and by the way, the reason this looks so evil is because I'm reading in a configuration file that has multiple types of parameters it defines (it's also inconsistent in it's definitions...I didn't write it) with variable arguments and lists for the different parameters. But I want to keep all of the information in a single map.

Maybe it'd be cleaner to create a general "configuration parameter" interface with multiple implementations?
Rob Marshall
Ranch Hand

Joined: Aug 17, 2009
Posts: 30
Hi Henry,

No, I guess you don't "gotta love it". I just thought it was funny that it actually worked

Here's another stab at it...I think this is better, but it could probably still be improved on:

In case it wasn't obvious, I'm new to Java...so sometimes I play with things just to see what happens. I guess I got a little carried away. Sorry.

When I compile the above I end up with a test_generics$Config.class, which I understand (I think), but I also get a test_generics$1.class, what's that?

Hi David,

I'm not sure I follow you. What do you mean by a "configuration parameter interface"? I am trying to figure out how to read in the config file and hold the information somewhere, but I will actually just create an instance of "ConfigFile" and use "get" methods to give me the pieces I need...Is that what you're talking about?

Thanks,

Rob
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
The $1 thing refers to anonymous classes inside your code. Each anonymous class gets it's own file with an incrementing number.

Using your old implementation you had some of them. I can't spot any anonymous classes in your code, maybe the class file is from the old implementation...
Rob Marshall
Ranch Hand

Joined: Aug 17, 2009
Posts: 30
Hi Sebastian,

I removed all the classes for test_generics.java and recompiled and the test_generics$1.class shows up again...

Thanks,

Rob
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19760
    
  20

HashMap and Map are in the end still generic, so the full declaration should be this:
First of all, notice how I removed the references to HashMap in the variable type and the generics. It makes it possible to switch to TreeMaps or LinkedHashMaps later on. Second, the generic value type for the inner map must be Object because you want to put anything in it. Anything else will not work.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Struggling with generics, "unchecked call to put()"