Hi,
I m writing code to map single key(Integer) with array of values. But, its not giving the correct output.
Here is the code:

import java.io.*;
import java.util.*;

class MapDemo{
public static void main(String args[]){

TreeMap<Integer, Object[]> tMap = new TreeMap<Integer, Object[]>();
   Object[] vals = {"tree", "map", "sorted"};
    tMap.put(1, vals);

System.out.println("Keys of tree map: " + tMap.keySet());
System.out.println("Values of tree map: " + tMap.values());



}
}

Recommended Answers

All 10 Replies

Please show the output and explain what is wrong with it and show what you want it to be.

Here the output I got:
Keys of tree map: [1]
Values of tree map: [[Ljava.lang.object;@3e25a5]

I want the output as:
Keys of tree map: [1]
Values of tree map: tree map sorted

[Ljava.lang.object;@3e25a5

This String is what is returned by the Object class's toString method for an array of Objects.
To get the contents of the objects in the array, you will have to write some code that knows the objects are Strings

Could you give that code here?

The Map contains an array. Get the array out of the Map
Write a loop to go thru the array.
Get each element from the array and cast it to a String

I will give it a try. Thanku.

Here u can use an object that contains all atributes writes. for example class Texts, that contains str1= "tree", str2="map", str3 = "sorted"... is necessary that u overwrite the method String toString() ... it can print a value overwrited ... In ur code, u obtain this: [Ljava.lang.object;@3e25a5] ... it's why u not overwrite the method toString. Here is an Example :

import java.io.*;
    import java.util.*;
    class MapDemo{
    public static void main(String args[]){
    Texts tx = new Texts("tree", "map", "sorted"); 
    TreeMap<Integer,Texts> tMap = new TreeMap<Integer, Texts>();
    
    tMap.put(1, tx);
     
    System.out.println("Keys of tree map: " + tMap.keySet());
    System.out.println("Values of tree map: " + tMap.values());
    }
    static class Texts{
    	private String str1="";
    	private String str2="";
    	private String str3="";
    	
    	public Texts(String str1,String str2,String str3){
    			this.str1=str1;
    		this.str2=str2;
    		this.str3=str3;
    	}
    	public String toString(){
    		return str1 + " " + str2 +" "+ str3;
    		}
        }
    }

ur output is here:
Keys of tree map: [1]
Values of tree map: [tree map sorted]

Post Solved.
atte,
Zolymo

Although that approach can work, at least for a fixed size list of values, it's a pretty heavy way to overcome the OP's problem which was how to print an array of Strings.
A better answer would be to use the Arrays.toString method, as in

System.out.println("Values of tree map: " + Arrays.toString(tMap.values()));

@James Have you tried your code? I get compiler errors:

TestCode7.java:364: cannot find symbol
symbol  : method toString(java.util.Collection<java.lang.Object[]>)
location: class java.util.Arrays
      System.out.println("Values of tree map: " + Arrays.toString(tMap.values()));
                                                        ^

Well now, don't I feel really stupid!
I focussed on the array of Objects, but forgot that they were embedded in a Collection. This code I have tested, although I can't help feeling there is a better way...

System.out.println("Values of tree map: " +  Arrays.deepToString(tMap.values().toArray()));
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.