Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: how to access a hashmap which is inside a MultiMap

  1. #1
    Junior Member
    Join Date
    Jun 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default how to access a hashmap which is inside a MultiMap

    I am having a Multimap which contains key as sitename and Values is another hashmap.
    I am getting these values from Database.

    LinkedHashMap> thisHash = new <LinkedHashMap();
        thisHash.put(fiscal_date,avg_RT);
        siteMap.put(site_name,thisHash);


    The sample value in my hashmap is


    Key: New York, Value: {06/13/2012=1.3}. Value: {06/14/2012=1.48}.
    Key: Bangalore, Value: {06/13/2012=2.53}. Value: {06/14/2012=3.08}.
    Key: San Jose, Value: {06/13/2012=1.25}. Value: {06/14/2012=1.28}.
    Key: Amsterdam, Value: {06/13/2012=1.71}. Value: {06/14/2012=2.27}.


    Now I need to access the LinkedHashMap which is inside this multimap and get the key and value seperately. Please help me to do the same.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: how to access a hashmap which is inside a MultiMap

    What is a multimap? Is that a class you have defined?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: how to access a hashmap which is inside a MultiMap

    Now I need to access the LinkedHashMap which is inside this multimap and get the key and value seperately.
    Reverse the steps you use to put the data into the collection. That is, take the collection and get the map associated with the site name. Then take this latter map and get whatever data you want from it. (eg the value for a given date, or the complete set of date/value pairs etc).

    -----

    If you get stuck, post code which at least compiles. Multimaps are not a standard Java collection, so identify what package you are getting this from (Google, Apache etc...).

  4. #4
    Junior Member
    Join Date
    Jun 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: how to access a hashmap which is inside a MultiMap

    MultiMap is a Apache Package.
    I am importing import org.apache.commons.collections.MultiMap.*;

    I have tried to get the Value of Collection into a HashMap. But as it is only 1 parameter, I am getting error.
    Set keySet = siteMap.keySet();
    Iterator keyIterator = keySet.iterator( );

    while( keyIterator.hasNext( ) ) {
    Object key = keyIterator.next( );
    System.out.print( "Key: " + key + ", " );

    Collection values = (Collection) siteMap.get( key );
    Iterator valuesIterator = values.iterator( );
    while( valuesIterator.hasNext( ) ) {
    LinkedHashMap Hash1 = new LinkedHashMap();
    Hash1 = valuesIterator.next();
    System.out.print( "Value: " + valuesIterator.next( ) + ". " );
    System.out.print("List value" + Hash1);
    }
    System.out.print( "\n" );
    }
    Here data types for Hash1 is LinkedHashMap and valuesIterator.next() is returning java.lang.object.

    Please help.
    Last edited by vidyasure; June 20th, 2012 at 12:38 AM.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: how to access a hashmap which is inside a MultiMap

    I am getting error.
    Please post the full text of the error message and show the code causing it.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Cannot get values from hashmap
    By uhertz in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 17th, 2011, 07:44 PM
  2. [SOLVED] Should i use a hashmap?
    By 6Sloth9 in forum Collections and Generics
    Replies: 2
    Last Post: May 1st, 2011, 08:58 PM
  3. Problem in HashMap
    By Hikari9 in forum Collections and Generics
    Replies: 2
    Last Post: April 19th, 2010, 10:44 PM
  4. Bitstrings vs Hashmap
    By April in forum Collections and Generics
    Replies: 3
    Last Post: February 2nd, 2010, 11:56 AM
  5. Default Access (package access) confusion
    By gauravrajbehl in forum Java Theory & Questions
    Replies: 1
    Last Post: November 18th, 2009, 04:11 AM