Results 1 to 6 of 6
  1. #1
    Ms.Ranjan is offline Member
    Join Date
    Jun 2008
    Posts
    85
    Rep Power
    0

    Default Retrieving all the keys corresponding to a value and vice versa

    Hi all

    I want to store multiple values in a single key?hashmap are for single key and value.I found that Multimap can do it.But my problem is given a duplicate value i should be able to retrieve all the key corresponding to it,given a value i should get its corresponding key and any other value associated with it

    For eg:I have a set of string like this

    Bob;ar;arizona
    smith;uk;united
    steve;us;united
    Bob;in;india

    given the value "united" i should be able to get "smith,steve"
    given the value "in" i should be able to get "Bob,india"

    Is there any possibilty i could implement this logic.any suggestion or ideas on how do i go abt doing this?Here is a code sample which i am trying to implement this

    Java Code:
    import org.apache.commons.collections.MultiMap;
    import org.apache.commons.collections.MultiHashMap;
    
    MultiMap map = new MultiHashMap( );
    
    map.put( "Bob", "ar" );
    map.put( "Bob", "arizona" );
    map.put( "smith", "uk" );
    map.put( "smith", "united" );
    map.put( "steve", "us" );
    map.put( "steve", "united" );
    map.put( "Bob", "in" );
    map.put( "Bob", "india" );
    
    Set keySet = map.keySet( );
    Iterator keyIterator = keySet.iterator( );
    while( keyIterator.hasNext( ) ) {
        Object key = keyIterator.next( );
    
        System.out.print( "Key: " + key + ", " );
    
        Collection values = (Collection) map.get( key );
        Iterator valuesIterator = values.iterator( );
        while( valuesIterator.hasNext( ) ) {
            System.out.print( "Value: " + valuesIterator.next( ) + ". " );
        }
    
        System.out.print( "\n" );
    }
    Output
    Key: Bob, Value: ar. Value: arizona. Value: in. Value: india.
    Key: steve, Value: us. Value: united.
    Key: smith, Value: uk. Value: united.

  2. #2
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    6

    Default

    can't think of an efficient way to do it. maps are meant to only be searched by key, so it would take a different approach to index by more than just the key.

  3. #3
    Ms.Ranjan is offline Member
    Join Date
    Jun 2008
    Posts
    85
    Rep Power
    0

    Default

    initially thought of going for multidimensional array?? but since the data is huge,i thought arrays wont be efficient way..any other workaround plz

  4. #4
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    It's not a map you want, it's a database. More abstractly you're trying to implement a ternary relation. One method of the top of my head would be to create your own class that encapsulates 3 maps. Something like this

    Java Code:
    public TernaryRelation<T1, T2, T3> {
        class Record<T1, T2, T3> {
            T1 column1;
            T2 column2;
            T3 column3;
        }
        private Map<T1, Record<T1, T2, T3>> index1;
        private Map<T2, Record<T1, T2, T3>> index2;
        private Map<T3, Record<T1, T2, T3>> index3;
    
        public void put(T1 arg1, T2 arg2, T3 arg3) {
            Record<T1, T2, T3> rec = new Record<T1, T2, T3>(arg1, arg2, arg3);
            index1.put(arg1, rec);
            index2.put(arg2, rec);
            index3.put(arg3, rec);
        }
    }
    You should get the gist. Obviously it needs more work before it will actually do stuff and you will probably want to tweak it to suit your needs. There are probably better ways of doing it but that's what I thought of just now.

    If the data is significantly large then you may want to use an external database instead and connect to it with JDBC.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  5. #5
    Ms.Ranjan is offline Member
    Join Date
    Jun 2008
    Posts
    85
    Rep Power
    0

    Default

    Thankyou very much..I will lookup for how to implement your idea,thanks i appreciate it,but if there are any other ways,plz do post it..

    Thanks

  6. #6
    Ms.Ranjan is offline Member
    Join Date
    Jun 2008
    Posts
    85
    Rep Power
    0

    Default

    Additional info:The string just does not have three elements it varies to four,five or just one element

Similar Threads

  1. converting image to binary code n vice versa
    By nupurashi in forum Advanced Java
    Replies: 4
    Last Post: 04-01-2009, 07:27 PM
  2. Replies: 0
    Last Post: 03-17-2009, 07:40 AM
  3. Postfix into prefix and vice versa
    By sfe23 in forum New To Java
    Replies: 9
    Last Post: 02-19-2009, 10:37 PM
  4. Converting to ASCII and vice-versa
    By pheonix in forum New To Java
    Replies: 2
    Last Post: 09-09-2008, 04:43 AM
  5. Document conversion PDF to MS doc and vice versa
    By abintoms in forum New To Java
    Replies: 1
    Last Post: 08-08-2007, 12:45 AM

Posting Permissions

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