1.HashSet:元素唯一,但是無序,他不保證set的迭代順序,也不保證順序的永恆不變。
注:如何保證元素的唯一性?在add源碼的方法中
if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {...}
左邊:e.hash==hash比較對象的哈希碼值
中間:(k = e.key) == key 比較對象的地址值
右邊:key.equals(k)比較對象的內容是否相同。
所以hashset中判斷元素是否唯一主要依賴hashcode和equals方法。判斷是,首先判斷哈希值是否相同,如果不同直接添加元素,如果相同,在判斷對象的equals方法,不同再添加,相同就說明元素重複,不做添加。所以要用hashset作集合時,要重寫hash code和equals方法,在java類中自動生成。
exp:存儲String類型的元素,說明元素無序且唯一
import java.util.HashSet;
public class hashDemo {
public static void main(String[] args) {
String s1="a";
String s2="b";
String s3="a";
HashSet<String> h=new HashSet<String>();
h.add(s1);
h.add(s2);
h.add(s3);
for (String s : h) {
System.out.println(s);
}
}
}//輸出b a或 a b
2.TreeSet:根據元素的自然順序對元素進行排序,或根據創建set時提供的comparator進行排序。
二叉樹:
exp:創建學生類對象,用treeset集合儲存並遍歷,學生對象爲標準java類,包含tostring方法的重寫
import java.util.Comparator;
import java.util.TreeSet;
public class StudentTest {
public static void main(String[] args) {
Student s1=new Student("a",1);
Student s2=new Student("b",2);
Student s3=new Student("c",3);
Student s4=new Student("a",1);
TreeSet<Student> t= new TreeSet<Student>(new Comparator<Student>() {
//匿名內部類傳參。
public int compare(Student o1, Student o2) {//自己規定比較規則。
int num1=o1.getAge()-o2.getAge();
int num2=num1==0?o1.getName().compareTo(o2.getName()):num1;
return num2;
}
});
t.add(s1);
t.add(s2);
t.add(s3);
t.add(s4);
for (Student s : t) {
System.out.println(s);
}
}
}//Student [name=a, age=1]
//Student [name=b, age=2]
//Student [name=c, age=3]有結果知,重複元素被刪除,並按照以重寫規則進行排序。
3.Set的遍歷:兩種方法,迭代器或者增強for循環。
4.hashset與treeset的不同:A:底層存儲的數據結構不同。分別爲hashmap和treemap。
B:存儲時保證數據唯一性的依據不同。前者時hashcode和equals方法,後者是
Compareable接口的compareTo()方法。
C:有序性不同,hashset無序,treeset有序。
5.Map:將鍵映射到值的對象,一個映射不能包含重複的鍵,每個鍵最多映射一個值。
方法:A:刪除功能:void clear():移除集合中的所有鍵值對元素
V remove(Object key):根據鍵移除鍵值對元素,並返回值
B:判斷功能:boolean containsKey(Object key):判斷集合中是否包含指定的鍵
boolean containsValue(Object value):判斷集合中是否包含指定的值
boolean isEmpty():判斷集合是否爲空
C:獲取功能:V get(Object key):根據鍵獲取值
Set<K> keySet():獲取所有的鍵
Collection<V> values():獲取所有的值
D:添加功能:V put(K key,V value):集合添加鍵值對
E:長度功能:int size():鍵值對對數。
6.HashMap:元素順序爲不可預測,底層算法爲哈希算法。
注:鍵是同一個對象的時候要把之前存入的元素擠出去,想要實現這樣的效果的話,
需要重寫javabean裏面的hashCode()和equals()方法。
exp:用hashmap存儲學生的鍵對值對象並遍歷。
public class Student {
private String name;
private int age;
public Student() {
super();
// TODO Auto-generated constructor stub
}
public Student(String name, int age) {
super();
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 + "]";
}
@Override
public int hashCode() {//重寫hashcode和equals方法,自動生成即可。
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@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;
}
}
import java.util.HashMap;
import java.util.Set;
public class StudentTest {
public static void main(String[] args) {
Student s1=new Student("a", 1);
Student s2=new Student("b", 2);
Student s3=new Student("c", 3);
HashMap<String, Student> h=new HashMap<String, Student>();
h.put("a", s1);
h.put("b", s2);
h.put("c", s3);
Set<String> key1=h.keySet();
for (String s : key1) {
System.out.println(s+" "+h.get(s));
}
System.out.println("-----------------------");
h.put("a", s2);//把s2覆蓋到key“a”上
Set<String> key2=h.keySet();
for (String s : key2) {//遍歷hashmap中的鍵值
System.out.println(s+" "+h.get(s));
}
}
/*b Student [name=b, age=2]
c Student [name=c, age=3]
a Student [name=a, age=1]
-----------------------
b Student [name=b, age=2]
c Student [name=c, age=3]
a Student [name=b, age=2]*/
}