Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TreeMap that contains String as key and a boolean as value?

Tags:

java

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.

like image 886
Spook Avatar asked Dec 10 '25 19:12

Spook


1 Answers

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

like image 143
dharam Avatar answered Dec 12 '25 08:12

dharam



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!