A Set in java never allows duplicates, but it takes StringBuffer objects with the same argument. Why?

后端 未结 6 1978
失恋的感觉
失恋的感觉 2021-01-12 21:55
public static void main(String[] args) {
    HashSet set = new HashSet(); 
    set.add(new StringBuffer(\"abc\"));
    set.add(new StringBuffer(\"abc\")); 
    set.a         


        
6条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-12 22:50

    Two StringBuffer objects are different objects despite having the same arguments. Therefore HashSet just adds the StringBuffers instead of ignoring duplicates.

提交回复
热议问题