What's the difference between HashSet and Set?

前端 未结 8 2007
独厮守ぢ
独厮守ぢ 2020-12-12 19:01

Saw the code snippet like

Set instances = new HashSet();

I am wondering if Hashset is a special kind of set. A

8条回答
  •  自闭症患者
    2020-12-12 19:35

    The question has been answered, but I haven't seen the answer to why the code mentions both types in the same code.

    Typically, you want to code against interfaces which in this case is Set. Why? Because if you reference your object through interfaces always (except the new HashSet()) then it is trivial to change the implementation of the object later if you find it would be better to do so because you've only mentioned it once in your code base (where you did new HashSet()).

提交回复
热议问题