java學習筆記(6)——Set 接口

Set接口

1.不包含不容許存儲重複元素
2.無索引,不能使用普通for循環遍歷多線程

Set集合在調用add方法時,add方法會調用元素的hashCode方法和equals方法,判斷元素是否重複。
若是發生hash衝突,則能夠用equals方法比較兩元素。spa

set集合存儲不重複元素原理:
image.png
前提:存儲的元素必須重寫hashCode方法和equals方法。線程

實現類:HashSet集合

特色:
1.不包含不容許存儲重複元素
2.無索引,不能使用普通for循環遍歷
3.無序集合
4.不一樣步多線程,速度快
5.底層是一個哈希表結構(查詢速度很是的快)code

Iterator<Integer> it = set.iterator();

        while(it.hasNext()) {
            Integer next = it.next();
            System.out.println(next);
        }
for (Integer integer : set) {
            System.out.println(integer);
        }

取出是按照從小到大取出???blog

相關文章
相關標籤/搜索