File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Generic for HashMap holding different values Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Generic for HashMap holding different values" Watch "Generic for HashMap holding different values" New topic
Author

Generic for HashMap holding different values

Praful Thakare
Ranch Hand

Joined: Feb 10, 2001
Posts: 641
Hi,
May be too basic question, but haven't worked much on generics,following is my scenario.

I have parentHash map that will hold one more child hashmap for every key, and this childhashmap can hold

1) HashMap <String,HashMap> or
2) HashMap<String,String>.

so how do i define parent hashmap using generics?

-P



All desirable things in life are either illegal, banned, expensive or married to someone else !!!
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

It is not possible for a generic HashMap to hold HashMaps and Strings (unless you're using a Object reference but that will nullify the advantages of generics). A solution would be do separate the 2 Maps in 2 classes and use a common interface to access them.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40029
    
  28
Praful Thakare wrote: . . . HashMap <String,HashMap> . . .
Map<String, Map>, surely?
Praful Thakare
Ranch Hand

Joined: Feb 10, 2001
Posts: 641
thanks much Wouter, thats cool solution.
Praful Thakare
Ranch Hand

Joined: Feb 10, 2001
Posts: 641
yes Campbell its HashMap<String, HashMap>

-P
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
Praful,

Campbell was trying to hint that it should be Map<String, Map>.

John.
Praful Thakare
Ranch Hand

Joined: Feb 10, 2001
Posts: 641
thanks John...had doubt abt it but was't sure , but now not able to understand the difference between HashMap<String, HashMap> and Map<Stgring,Map> can you explain ?
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
Praful:

It's the idea of programming against interfaces, rather than implementations. For example, if I do this:

I'm locking myself into an implementation - which may at some later point be difficult to change, or may be impossible to change if I expose methods in a public API.

If I do this:

I can be assured that if I need to make a change at a later date (say, like it turns out that a TreeMap would work better), then all I have to do is change that one line of code, and I don't have to worry about my class' public API.

John.
Jinglong Wang
Greenhorn

Joined: Mar 05, 2010
Posts: 12
maybe you can
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generic for HashMap holding different values