I saw the following recently on this site:
for (HashMap.Entry<Object,Object> e : new TreeMap<>().entrySet())
System.out.println(e);
To my surprise, this compiles and runs fine. I have also tried adding entries to the map so there is actually something to downcast and fail doing so, this worked fine as well. How can a TreeMap
entry be cast to HashMap.Entry
? These two aren't even on the same branch of the hierarchy.
Although this matter is resolved now, I include the following just for fascination—the following does not compile:
for (TreeMap.Entry<Object,Object> e : new HashMap<>().entrySet())
System.out.println(e);
It happens that TreeMap
defines TreeMap.Entry
, which hides Map.Entry
.
You feel as if you do not enjoy time togetherIf you dread time with your partner or feel that you do not have fun together, you may be in a confusing relationship. Time spent with someone you love should be enjoyable, and you shouldn't feel like you are trying to force yourself to have a good time.
There are many different types of relationships. This section focuses on four types of relationships: Family relationships, Friendships, Acquaintanceships and Romantic relationships.
This a classic example of what a complicated relationship means. More reasons include that one of you is married and you are having an affair, you are working together, your friends or family don't approve or think this person is good for you, and the list goes on.
Although you are accessing it on HashMap
, Entry
is actually a (implicitly static
) member type declared in Map
. TreeMap#entrySet
has a return type of Set<Map.Entry<K,V>>
. This is the same Entry
type.
new TreeMap<>().entrySet() returns Set<Map.Entry<K,V>>
and you are iterating over every entry of the set (Entry<Object, Object>)
. Hence it compiles. In java 8 you can replace the for loop like
new TreeMap<>().entrySet().forEach(System.out::println);
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