【JavaSE筆記】集合(三)_Set

本期知識點
Set及其子類
Collation總結


1.Set

a.Set集合的特點:

無序(存儲和取出不一致),集合中的元素不可以重複。

import java.util.HashSet;
import java.util.Set;

public class Demo01 {
	public static void main(String[] args) {
		//創建Set集合對象
//		Set是接口不能直接實例化
//		Set<String> a = new Set<String>();
		Set<String> a = new HashSet<String>();
		a.add("hello");
		a.add("is");
		a.add("me");
		a.add("is");
		a.add("me");
		a.add("hello");
		//增強for遍歷
		for (String s : a) {
			System.out.println(s);
		}
	}
}
/*out:
me
is
hello
*/

2.HashSet

a.底層數據結構是哈希表(是一個元素爲鏈表的數組)

b.哈希表底層依賴兩個方法:

hashCode()equals()
執行順序:
首先比較哈希值是否相同?
相同:
繼續執行equals()方法:
true:元素重複了,不添加
false:直接把元素添加到集合中
不同:
直接把元素添加到集合中

c.如何保證元素的唯一性?

由hashCode()、equals()保證。

e.HashSet存儲字符串並遍歷

import java.util.HashSet;

public class HashSet存儲字符串並遍歷 {
	public static void main(String[] args) {
		//創建HashSet集合對象;
		HashSet<String> a = new HashSet<String>();
		a.add("hello");
		a.add("is");
		a.add("me");
		a.add("hello");
		a.add("is");
		a.add("me");
		
		for (String s : a) {
			System.out.println(s);
		}
	}
}
/*out:
	me
	is
	hello*/

f.HashSet存儲自定義對象並遍歷

import java.util.HashSet;
class Student{
	private String name;
	private int age;
	
	public Student() {
		super();
	}
	public Student(String name, int age) {
		this.name = name;
		this.age = age;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	@Override
	public String toString() {
		return "Student [name=" + name + ", age=" + age + "]";
	}
	//重寫equals方法 比較的是內容
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Student other = (Student) obj;
		if (age != other.age)
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}
}
public class HashSet存儲自定義對象並遍歷 {
	public static void main(String[] args) {
		//創建HashSet集合對象
		HashSet<Student> a = new HashSet<Student>();
		//添加對象
		a.add(new Student("Tom",21));
		a.add(new Student("Peter",20));
		a.add(new Student("Tom",21));
		a.add(new Student("Jack",23));
		//遍歷
		for (Student s : a) {
			System.out.println(s.getName()+"---"+s.getAge());
		}
	}
}
/*out:
	Tom---21
	Tom---21
	Peter---20
	Jack---23*/

3.TreeSet

a.底層數據結構是紅黑樹(自然平衡二叉樹)

b.保證元素的排序方式:

i.自然排序(元素具備比較性)
讓元素所屬的類實現 Comparable 接口
ii.比較器排序(集合具備比較性)
讓集合構造方法接收 Comparator的實現類對象

c.如何保證元素的唯一性?

根據比較的返回值是否是0來決定
import java.util.TreeSet;

public class Demo01 {
	public static void main(String[] args) {
		//創建TreeSet對象
		TreeSet<Integer> a = new TreeSet<Integer>();
		//給集合添加元素
		//3,2,5,8,4,7,6
		a.add(3);
		a.add(2);
		a.add(5);
		a.add(8);
		a.add(4);
		a.add(7);
		a.add(6);
		//遍歷
		for (Integer i : a) {
			System.out.print(i+" ");
		}
	}
}
//out:2 3 4 5 6 7 8 

4.Collection集合總結:

Collection
|--List 有序,可重複
|--ArrayList
底層數據結構是數組,查詢快,增刪慢。
線程不安全,效率高
|--Vector
底層數據結構是數組,查詢快,增刪慢。
線程安全,效率低
|--LinkedList
底層數據結構是鏈表,查詢慢,增刪快。
線程不安全,效率高
|--Set 無序,唯一
|--HashSet
底層數據結構是哈希表。
如何保證元素唯一性的呢?
依賴兩個方法:hashCode()和equals()
開發中自動生成這兩個方法即可
|--LinkedHashSet
底層數據結構是鏈表和哈希表
由鏈表保證元素有序
由哈希表保證元素唯一
|--TreeSet
底層數據結構是紅黑樹。
如何保證元素排序的呢?
自然排序
比較器排序
如何保證元素唯一性的呢?
根據比較的返回值是否是0來決定


5.Collection集合我們到底使用誰呢?

唯一?(不確定?用ArrayList)
是:Set
排序?(不確定,用HashSet)
是:TreeSet
否:HashSet
否:List
安全?(不確定,用ArrayList)
是:Vector
否:
查詢多:ArrayList
增刪多:LinkedList


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章