Hello I tried to create a TreeMap in Java but the compiler gives the error saying "unexpected type". Can anyone tell me why? Isn't possible to have primitive data types in a Map?
I did solve it by using the java.lang.Boolean instead. But I don't understand why I can't mix a complex data type with a primitive one.
To answer your query,
Maps and other classes which are treated as collections not only serve the purpose of storing data but they also provide methods for comparing. In java comparators are used for the same. In short you need to call methods on the objects you want to compare. If the values are primitive data type, no methods can be invoked on them. This is precisely one of the major reasons why primitive data types are not permitted in collections and related classes.
hope this helps
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With