集合是數(shù)據(jù)存儲(chǔ)中,重要的容器對(duì)象设联。
單列集合
- Collection 單例集合根接口
- List 實(shí)現(xiàn)了List接口的集合類 特點(diǎn): 有序捌年,可重復(fù)
- ArrayList 實(shí)現(xiàn)了List接口 底層使用數(shù)組 特點(diǎn):速度快,增刪慢
- LinkedList 底層使用鏈表數(shù)據(jù)結(jié)構(gòu), 特點(diǎn): 查詢速度慢,增刪快
- Vector 底層使用數(shù)組 特點(diǎn):對(duì)比ArrayList線程安全,效率低
- Set 實(shí)現(xiàn)了Set接口的集合類 特點(diǎn): 無(wú)序, 不可重復(fù)
- HashSet 底層使用Hash表實(shí)現(xiàn), 特點(diǎn): 存取速度快
- TreeSet 底層使用二叉樹實(shí)現(xiàn) 特點(diǎn): 排序存儲(chǔ)
- List 實(shí)現(xiàn)了List接口的集合類 特點(diǎn): 有序捌年,可重復(fù)
<pre>注意: HashSet <h6>1.HashSet先調(diào)用HashCode方法算出Hash值,對(duì)比Hash值.相同進(jìn)行第二步,建議重寫HashCode方法
</h6><h6>2.調(diào)用對(duì)象的Equals方法,建議重寫Equals方法</h6>
</pre>
<pre>注意:TreeSet<h6>1.元素具備自然排序的特點(diǎn),就按照自然順序進(jìn)行排序</h6><h6>2.如果元素不具備自然排序的特點(diǎn),那么元素需要實(shí)現(xiàn)Comparable接口,自定義比較規(guī)則,重寫CompareTo方法</h6></pre>
雙列集合
- Map 鍵值對(duì)存儲(chǔ) 鍵不可重復(fù),值可重復(fù)
- HashMap 底層哈希表
- TreeMap 底層二叉樹
ArrayList Demo
public static void main(String[] args) {
ArrayList<String> array = new ArrayList<String>();
array.add("First");
array.add("Second");
array.add("Three");
for (int i = 0; i < array.size(); i++) {
System.out.println(array.get(i));
}
}
Console
First
Second
Three
HashSet Demo
public static void main(String[] args) {
HashSet<String> hSet = new HashSet<>();
hSet.add("First");
hSet.add("Second");
hSet.add("Three");
// Iterator 遍歷
Iterator<String> it = hSet.iterator();
while(it.hasNext()){
System.out.print(it.next() + ",");
}
}
// 注意 :while中不能直接修改元素個(gè)數(shù)
Console
Second,First,Three,
HashMap Demo
public static void main(String[] args) {
HashMap<String,Integer> map = new HashMap<>();
map.put("Fisrt", 1);
map.put("Second",2);
map.put("Three", 3);
Set<Entry<String,Integer>> entrys= map.entrySet();
// 遍歷
for (Entry<String, Integer> entry : entrys) {
System.out.println("Key :" + entry.getKey() + "Value :" + entry.getValue());
}
}
Console
Key :SecondValue :2
Key :FisrtValue :1
Key :ThreeValue :3
給個(gè)github follow me的鏈接,上面有很多初學(xué)者可供學(xué)習(xí)的資料谴轮,項(xiàng)目.
<a>https://github.com/SuperZee</a>