Java relay creation object

Set < String > set = new HashSet < > ();
Why do you often see such new objects? what are the advantages of
compared with
HashSet < String > set = new HashSet < > ();
?

Feb.27,2021

personal concerns
for Set < String > set = new HashSet < > () , Set is actually an interface, and the interface doesn't care about what the object is, whether it's HashSet,TreeSet or anything else. It is the embodiment of the idea of interface-oriented programming. The advantage of
is that if one day you find that HashSet is not suitable for this location, you need to change it to TreeSet, with sorting function or CopyOnWriteHashSet, that needs to be changed to thread-safe. You only need to modify the way it is created, and you don't need to modify the code in other locations.

Menu