Results 1 to 20 of 20
  1. #1
    Mathan is offline Member
    Join Date
    Jun 2008
    Posts
    8
    Rep Power
    0

    Thumbs up 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)

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  3. #3
    Mathan is offline Member
    Join Date
    Jun 2008
    Posts
    8
    Rep Power
    0

    Default

    getPas() method will read from local cache. It is throwing Casting Exception. Do you need any more information?

  4. #4
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    6

    Default

    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.

  5. #5
    Mathan is offline Member
    Join Date
    Jun 2008
    Posts
    8
    Rep Power
    0

    Default

    yes, here the key value is "Name" as already i given the same. Still i am getting problem

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  7. #7
    Mathan is offline Member
    Join Date
    Jun 2008
    Posts
    8
    Rep Power
    0

    Default

    i cannot understand

  8. #8
    daGame is offline Member
    Join Date
    May 2008
    Posts
    24
    Rep Power
    0

    Default

    Quote Originally Posted by Mathan View Post
    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 :mad:
    Last edited by daGame; 06-26-2008 at 11:36 AM.

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  10. #10
    daGame is offline Member
    Join Date
    May 2008
    Posts
    24
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post
    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? :D

  11. #11
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    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.

  12. #12
    Mathan is offline Member
    Join Date
    Jun 2008
    Posts
    8
    Rep Power
    0

    Default

    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?

  13. #13
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    6

    Default

    Java Code:
    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 =....

  14. #14
    Mathan is offline Member
    Join Date
    Jun 2008
    Posts
    8
    Rep Power
    0

    Default

    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[]>

  15. #15
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    String[] is not absolutely equal to Object.....
    freedom exists in the world of ideas

  16. #16
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by sukatoa View Post
    String[] is not absolutely equal to Object.....
    Yep, this is what you have going wrong here.

  17. #17
    Mathan is offline Member
    Join Date
    Jun 2008
    Posts
    8
    Rep Power
    0

    Default

    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

  18. #18
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    6

    Default

    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?

  19. #19
    Mathan is offline Member
    Join Date
    Jun 2008
    Posts
    8
    Rep Power
    0

    Default

    Yes this is not a solution. thanks for your helping. yes i did System.out. That is not printing anything.

  20. #20
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    6

    Default

    Didn't print anything? I've never heard of that.
    Try changing it to "hello world" and make sure that its getting executed.

Similar Threads

  1. Which concept used in java for monitoring the printer
    By kalinga2k4 in forum Networking
    Replies: 1
    Last Post: 06-14-2008, 01:25 AM
  2. mail concept
    By indirani in forum New To Java
    Replies: 3
    Last Post: 04-16-2008, 01:30 PM
  3. mail concept
    By thamizhisai in forum Advanced Java
    Replies: 4
    Last Post: 04-11-2008, 07:19 AM
  4. What is RMI concept in Spring Framework
    By Java Tip in forum Spring Framework
    Replies: 0
    Last Post: 04-02-2008, 10:36 AM
  5. easy way to study the java springs concept
    By kumar84 in forum New To Java
    Replies: 1
    Last Post: 07-17-2007, 03:53 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •