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

    Default Retrieve array inside a list as a Map value

    Hi All,

    I am trying to retrieve data from a map value. One of the map value is actually an array inside a list. I am able to get only the memory location rather than the values inside the array inside the list. Below is the code that I am working on. Can someone please help me

    Java Code:
    import java.util.Collection;
    import java.util.Iterator;
    import java.util.LinkedList;
    import java.util.List;
    import java.util.Map;
    import java.util.Set;
    
    import org.apache.commons.collections.*;
    import org.apache.commons.collections.collection.*;
    import org.apache.commons.collections.MultiMap;
    import org.apache.commons.collections.MultiHashMap;
    import org.apache.commons.collections.map.*;
    
    public class Names {
    	public static void main(String args[])
    	{
         List result = new LinkedList();
         
         String[] vs= {"name1","name2"};
    	
         result.add(vs);
         
    	MultiMap map = new MultiHashMap( );
    	map.put("ONE","TEST");	
    	map.put("ONE",1);
    	map.put("ONE", result);
    	
    	/*Set set = map.entrySet();
        Iterator it = set.iterator();
        while (it.hasNext()) {
          Map.Entry entry = (Map.Entry) it.next();
          System.out.println(entry.getKey() + " : " + entry.getValue());
        }*/
    
    	
    	Set keySet = map.keySet( );
    	Iterator keyIterator = keySet.iterator();	
    	while( keyIterator.hasNext( ) ) {
    	    Object key = keyIterator.next( );
    	    System.out.println( "Key: " + key);
    	    
    	    Collection values = (Collection) map.get( key );
    	   
    	    	 Iterator valuesIterator = values.iterator( );
    	 	    while( valuesIterator.hasNext( ) ) {
    	 	    	
    	 	       System.out.println( "Value: " + valuesIterator.next( ));
    	 	        
    	 	    }
               }	    
    	    
    	}
    
    }
    The output that I get is
    Java Code:
    Key: ONE
    Value: TEST
    Value: 1
    Value: [[Ljava.lang.String;@9304b1]
    Output Expected
    Java Code:
    Key: ONE
    Value: TEST
    Value: 1
    Value: name1
    Value: name2
    Thanks

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,572
    Rep Power
    25

    Default

    Value: [[Ljava.lang.String;@9304b1]
    \
    This is what the default toString() method returns for an object without its own toString() method.
    It says: [ = an array, L =~ List of String(??)

    I think The value of ONE is an array which doesn't have a toString() method, so you get the default toString() output. You might need to extend the class that the array is in and add a toString() method to return its contents vs getting the default value.

    Your various changes of definitions loses the id of the contents. Here's how to get what't there:
    Java Code:
    /*
    I am trying to retrieve data from a map value. One of the map value is actually an array inside a list. 
    I am able to get only the memory location rather than the values inside the array inside the list. 
    Below is the code that I am working on. Can someone please help me 
    
    The output that I get is 
    
    Key: ONE
    Value: TEST
    Value: 1
    Value: [[Ljava.lang.String;@9304b1]
    
    Output Expected
    Key: ONE
    Value: TEST
    Value: 1
    Value: name1
    Value: name2
    
    */
    
    import java.util.*;
    
    
    public class ToStringProblem {
    
    	public static void main(String args[])  	{
          List result = new LinkedList();
         
          String[] vs= {"name1","name2"};
    	
          result.add(vs);
         
    	   HashMap map = new HashMap( );
    	   map.put("ONE", result);
    	
       	Set keySet = map.keySet( );
       	Iterator keyIterator = keySet.iterator();	
    
       	while( keyIterator.hasNext( ) ) {
       	    Object key = keyIterator.next( );
       	    System.out.println( "Key: " + key);
       	    
       	    Collection values = (Collection) map.get( key );
       	   
           	 Iterator valuesIterator = values.iterator( );
        	    while( valuesIterator.hasNext( ) ) {
        	    	 Object obj = valuesIterator.next();
                 String[] sArray = (String[]) obj;
        	       System.out.println( "Value: " + obj 
                                     + ", array=" + Arrays.toString(sArray)); 
                 // Key: ONE
                 // Value: [Ljava.lang.String;@19821f, array=[name1, name2]
        	        
        	    }
          } // end while()	    
    	    
       }
    
    }
    Last edited by Norm; 07-22-2010 at 06:50 PM. Reason: Added code

  3. #3
    Ramanjaneyulu is offline Member
    Join Date
    Aug 2010
    Posts
    11
    Rep Power
    0

    Default

    Hi Ms.Ranjan,
    Please check the modified code to print according to your requirement

    import java.util.Collection;
    import java.util.Iterator;
    import java.util.LinkedList;
    import java.util.List;
    import java.util.Map;
    import java.util.Set;

    import org.apache.commons.collections.*;
    import org.apache.commons.collections.collection.*;
    import org.apache.commons.collections.MultiMap;
    import org.apache.commons.collections.MultiHashMap;
    import org.apache.commons.collections.map.*;

    public class Names {
    public static void main(String args[])
    {
    List result = new LinkedList();

    String[] vs= {"name1","name2"};

    result.add(vs);

    MultiMap map = new MultiHashMap( );
    map.put("ONE","TEST");
    map.put("ONE",1);
    map.put("ONE", result);

    /*Set set = map.entrySet();
    Iterator it = set.iterator();
    while (it.hasNext()) {
    Map.Entry entry = (Map.Entry) it.next();
    System.out.println(entry.getKey() + " : " + entry.getValue());
    }*/


    Set keySet = map.keySet( );
    Iterator keyIterator = keySet.iterator();
    while( keyIterator.hasNext( ) ) {
    Object key = keyIterator.next( );
    System.out.println( "Key: " + key);

    Collection values = (Collection) map.get( key );

    Iterator valuesIterator = values.iterator( );
    while( valuesIterator.hasNext( ) ) {
    Object obj=valuesIterator.next( );
    if(obj instanceof LinkedList){
    LinkedList list=(LinkedList)obj;
    String[] x=(String[])list.get(0);
    System.out.print( "Value: ");
    for(int i=0;i<x.length;i++)
    System.out.print( x[i]+" ");
    }
    else
    System.out.println( "Value: " + obj);
    }
    }

    }

    }

Similar Threads

  1. Linked list inside a linked list
    By viperlasson in forum New To Java
    Replies: 5
    Last Post: 07-26-2010, 11:15 PM
  2. 2D array inside a HashMap
    By darkblue24 in forum New To Java
    Replies: 1
    Last Post: 03-23-2010, 12:39 AM
  3. How do I retrieve an array from a different class?
    By Psyclone in forum AWT / Swing
    Replies: 11
    Last Post: 02-08-2010, 09:52 AM
  4. Retrieve Multidimensional Array ??
    By oneofthelions in forum New To Java
    Replies: 3
    Last Post: 12-12-2009, 07:24 AM
  5. Retrieve values of Text boxes using LIST
    By Kayal in forum Web Frameworks
    Replies: 2
    Last Post: 03-20-2009, 11:00 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
  •