Hashset vs Treeset

后端 未结 14 1771
再見小時候
再見小時候 2020-11-22 13:38

I\'ve always loved trees, that nice O(n*log(n)) and the tidiness of them. However, every software engineer I\'ve ever known has asked me pointedly why I would u

14条回答
  •  孤街浪徒
    2020-11-22 14:04

    1.HashSet allows null object.

    2.TreeSet will not allow null object. If you try to add null value it will throw a NullPointerException.

    3.HashSet is much faster than TreeSet.

    e.g.

     TreeSet ts = new TreeSet();
     ts.add(null); // throws NullPointerException
    
     HashSet hs = new HashSet();
     hs.add(null); // runs fine
    

提交回复
热议问题