|
|
Welcome to the Java Forums.
You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:
- have access to post topics
- communicate privately with other members (PM)
- not see advertisements between posts
- have the possibility to earn one of our surprises if you are an active member
- access many other special features that will be introduced later.
Registration is fast, simple and absolutely free so please, join our community today!
If you have any problems with the registration process or your account login, please contact us.
|
|

06-25-2008, 07:14 AM
|
|
Member
|
|
Join Date: Jun 2008
Posts: 8
|
|
|
Map concept in JAVA
Hi,
I am new to JAVA. I have some problem for using map concept.
I am calling getPas method & returning like this.
Map<String,Object> param = getPas(string xyz, String[] Name);
String[] config = (String[]) param.get(Name);
using the Name argument i am fetching the values from Map and casting to String[]. So if the param.get() returns more than one value( like ABC, DEF, KLM, OPQ...) it is working fine. But it return single value( like ABC), it is not storing to config variable, instead throwing error ( type casting error)
Can any one give me the better solution?
Note : i have one more function :
Map<String,Object> param = getPas(string xyz, String[] Name);
String config = (String) param.get(Name);
In this function will not work for String[] means more than one return value( like ABC, DEF, KLM, OPQ...). Thats why i created one new function( the top function)
|
|

06-25-2008, 07:49 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,609
|
|
|
What your getPas() do? And what error throw by your code?
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

06-25-2008, 08:13 AM
|
|
Member
|
|
Join Date: Jun 2008
Posts: 8
|
|
|
getPas() method will read from local cache. It is throwing Casting Exception. Do you need any more information?
|
|

06-25-2008, 08:27 AM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Posts: 476
|
|
|
Make it simpiler until you get it working.
Use the standard get() function, which takes one parameter, the key
Once that works, you can wrap it in other code.
|
|

06-25-2008, 08:32 AM
|
|
Member
|
|
Join Date: Jun 2008
Posts: 8
|
|
|
yes, here the key value is "Name" as already i given the same. Still i am getting problem
|
|

06-25-2008, 09:08 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,609
|
|
|
As fishtoprecords says, in simpler way is that working?
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

06-26-2008, 12:55 PM
|
|
Member
|
|
Join Date: Jun 2008
Posts: 8
|
|
|
i cannot understand
|
|

06-26-2008, 01:32 PM
|
|
Member
|
|
Join Date: May 2008
Posts: 24
|
|
Originally Posted by Mathan
i cannot understand
Test the method using normal Map without using the generics and get it
using the normal get() method. First get this running and then move on to
use generics, generics can get tricky as i've found out, you have to use the
casts properly.
"Use the standard get() function, which takes one parameter, the key" - fishtoprecords
Read the api for maps. It has a function which takes only the key parameter
WTF i cant post any links....this is terrible 
Last edited by daGame : 06-26-2008 at 01:36 PM.
|
|

06-26-2008, 01:42 PM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,609
|
|
|
generics cause damages on you for latest versions. So better to move with latest version.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

06-26-2008, 01:56 PM
|
|
Member
|
|
Join Date: May 2008
Posts: 24
|
|
Originally Posted by Eranga
generics cause damages on you for latest versions. So better to move with latest version.
Generics are the latest version. Generics are supported from jdk 1.5 onwards
Are we missing something mod? 
|
|

06-26-2008, 02:10 PM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,609
|
|
Oops, a big mistake. That word should be change.
generics cause damages on you for early versions. So better to move with latest version.
Really sorry about my mistake.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

06-26-2008, 05:05 PM
|
|
Member
|
|
Join Date: Jun 2008
Posts: 8
|
|
|
Hi all,
i am getting this exception for above query.
"java.lang.ClassCastException: java.lang.String"
See the code :
String[] configParam = null;
Map<String, Object> paramMap = getNetworkPropertiesFromPas(packageName,
new String[] {parameterName});
if (paramMap != null) {
try{
Set keys = paramMap.keySet(); // The set of keys in the map.
Iterator keyIter = keys.iterator();
while (keyIter.hasNext()) {
String key = (String)keyIter.next(); // Get the next key.
configParam = (String[])paramMap.get(key); // Get the value for that key.
for( int f=0; f < configParam.length; f++ ) {
System.out.println( ".....................Value[" + f + "] is = (" + configParam[f] + ")" );
}
}
}
catch(Exception e){
System.out.println("Exception e is = " + e);
}
}
Here : configParam = (String[])paramMap.get(key);
If the configParam value is more than one value means (like configParam[0]=abc, configParam[1]=def, configParam[2]=ijk) its working fine.
But the value is single means (like configParam[0]=abc) throwing exception like "java.lang.ClassCastException: java.lang.String"
Can any one give me the quick solution?
|
|

06-26-2008, 05:19 PM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Posts: 476
|
|
Map<String, Object> paramMap =....
(snip)
configParam = (String[])paramMap.get(key); // Get the value for that key.
You should chante the code to look like
Object oFromMap = paramMap.get(key);
System.out.println(oFromMap.getClass().getName();
so you can see what you are getting. Don't cast blindly.
And if you really are placing an array of String in the map, declare it that way.
Map<String, String[]t> paramMap =....
|
|

06-26-2008, 05:46 PM
|
|
Member
|
|
Join Date: Jun 2008
Posts: 8
|
|
|
Hi,
I tried "Map<String, String[]t> paramMap =...." this...
But i got the below syntax error.
Type mismatch: cannot convert from HashMap<String,Object> to Map<String,String[]>
|
|

06-26-2008, 06:20 PM
|
 |
Senior Member
|
|
Join Date: Jan 2008
Location: Cebu City, Philippines
Posts: 527
|
|
|
String[] is not absolutely equal to Object.....
__________________
A specific, detailed, simple, well elaborated, and "tested before asking" question may gather more quick replies. hopefully To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

06-27-2008, 06:17 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,609
|
|
Originally Posted by sukatoa
String[] is not absolutely equal to Object.....
Yep, this is what you have going wrong here.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

06-27-2008, 07:16 AM
|
|
Member
|
|
Join Date: Jun 2008
Posts: 8
|
|
|
That is Mr.Fishtoprecords suggestion . so i tried that way also.... not helping. Can any one tell me the good way?
And one thing i can not change to String[] in the Map argument <String, String[]>. because already lot of other functions using only object <String, Object>. So give me better solution ASAP
|
|

06-27-2008, 07:24 AM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Posts: 476
|
|
|
I won't give you a solution. I will help.
Did you do the System.out.println that I suggested upthread? What did it tell you? How do you use the information it gave you?
|
|

06-27-2008, 07:59 AM
|
|
Member
|
|
Join Date: Jun 2008
Posts: 8
|
|
|
Yes this is not a solution. thanks for your helping. yes i did System.out. That is not printing anything.
|
|

06-27-2008, 08:02 AM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Posts: 476
|
|
|
Didn't print anything? I've never heard of that.
Try changing it to "hello world" and make sure that its getting executed.
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|