The difference is that Collection is an interface, but String is a concrete class. When the compiler sees (b instanceof Collection), it knows that the declared type of b is TreeMap, which does not implement Collection. But (as far as the compiler knows) b may actually be an instance of some subclass of TreeMap which does implement Collection. I don't want to imagine what sort of poorly-conceived design such a class would have, but it's theoretically possible. A subclass of TreeMap could implement any interface as far as the compiler is concerned (though some may be impossible due to method naming collisions, but the compiler isn't going to analyze that particular point here).
On the other hand when the compiler sees (b instanceof String), it knows b is a TreeMap, and that no subclass of TreeMap could ever possibly be a String. They are unrelated types. (Plus String is final.) So the compiler flags this line as an error.