This does not return anything to me: System.out.println(tree.values());
, the castings do not work either, ie System.out.println((int)tree.values());
How do I get it to print the collection of key values?
Map<Integer,String> tree=new TreeMap<>();
tree.values();//Devuelve una colección de los valores contenidos en
//este mapa.
System.out.println(tree.values());
values
The class methodTreeMap
returns an object of typeCollection
so it can't be printed like that unless you do antoString
al to itcollection
:System.out.println(tree.values().toString())
To print a
Collection
, just go through it, you can use theforeach
, in your case as the values are of typeString
, it would look like this:If what you want is to print the
keys
, you can use the methodtree.keySet()
that returns aSet
that at the end also inherits from Collection so you can go through it in the same way:As an addition to Ali's answer, using Java 8: