java-day31
文章目錄
SortedSet接口有一個實現類:TreeSet
通過Comparable接口實現比較
在java中,實現了Comparable接口的類的對象,是可以比較大小的,通過調用接口中的方法compareTo方法就可以比較倆個對象的大小。在compareTo方法中, 可以按照自己定義的規則來比較倆個對象的大小。
例子:
重寫Student裏面的compareTo方法使其按照年齡排序(從小到大)
//重寫compareTo方法
//s1.compareTo(s2)
public int compareTo(Object obj){
//this:s1 obj:s2
//s1 < s2 return <0
//s1 > s2 return >1
//s2 = s2 return 0
Student stu =(Student) obj;
return this.age - stu.age;
}
重寫Student裏面的compareTo方法使其按照年齡排序(從大到小)
//重寫compareTo方法
//s1.compareTo(s2)
public int compareTo(Object obj){
//this:s1 obj:s2
//s1 < s2 return <0
//s1 > s2 return >1
//s2 = s2 return 0
Student stu =(Student) obj;
return -(this.age - stu.age);
}
重寫Student裏面的compareTo方法使其按照id排序(從小到大)
//重寫compareTo方法
//s1.compareTo(s2)
public int compareTo(Object obj){
//this:s1 obj:s2
//s1 < s2 return <0
//s1 > s2 return >1
//s2 = s2 return 0
Student stu =(Student) obj;
return (int)(this.id - stu.id);
}
重寫Student裏面的compareTo方法使其按照id排序(從大到小)
//重寫compareTo方法
//s1.compareTo(s2)
public int compareTo(Object obj){
//this:s1 obj:s2
//s1 < s2 return <0
//s1 > s2 return >1
//s2 = s2 return 0
Student stu =(Student) obj;
return -(int)(this.id - stu.id);
}
注意:
在比較大小的時候我們是在computerTo調用者對象的角度來看比較大小的問題
//站在s1的角度來看大小問題
//s1.compareTo(s2)
public int compareTo(Object obj){
// this obj
}
通過比較器Comparator實現比較
一個類實現了Comparable接口之後就可以進行對象的大小比較了TreeSet的構造器中還可以傳一個比較器Comparator,這個比較器也可以比較對象之間的大小,即使對象沒有實現Comparable接口也是可以比較大小的。
TreeSet中的構造器:
public TreeSet( Comparator comparator )
如果集合中的對象實現了Comparable,同時TreeSet中有設置了比較器Comparator,倆者都存在的情況下,比較器的優先級更高。
public void test4(){
Set set1 =null;
Comparator c = new Comparator(){
public int compare(Object o1,Object o2){
Integer a1 = (Integer) o1;
Integer a2 = (Integer) o2;
return -a1.compareTo(a2);
}
};
set1 = new TreeSet(c);
set1.add(1);
set1.add(2);
set1.add(6);
set1.add(4);
set1.add(5);java
this.showCollection(set1);
}
自定義排序順序
給定一個隨機數,當這個隨機數大於5的時候進行從小到大排序,否則進行從大到小排序
public void test4(){
Set set1 =null;
Comparator c = null;
int x = (int)(Math.random()*10);
System.out.println("x="+x);
if(x >= 5){
System.out.println("從小到大");
c = new Comparator(){
public int compare(Object o1,Object o2){
Integer a1 = (Integer) o1;
Integer a2 = (Integer) o2;
return a1.compareTo(a2);
}
};
}
else{
System.out.println("從大到小");
c = new Comparator(){
public int compare(Object o1,Object o2){
Integer a1 = (Integer) o1;
Integer a2 = (Integer) o2;
return -a1.compareTo(a2);
}
};
}
set1 = new TreeSet(c);
set1.add(1);
set1.add(2);
set1.add(16);
set1.add(26);
set1.add(36);
set1.add(46);
set1.add(64);
set1.add(4);
set1.add(3);
set1.add(0);
set1.add(5);
this.showCollection(set1);
}
先排偶數(從大到小),再排奇數(從小到大)
public void test5(){
Set set1 =null;
Comparator c = new Comparator(){
public int compare(Object o1,Object o2){
int a1 = ((Integer) o1).intValue();
int a2 = (Integer) o2;//自動拆箱
//a1是偶數 a2是偶數
if(a1%2==0 && a2%2==0){
return -(a1-a2);//都是偶數,從大到小拍
}
//a1是偶數 a2是奇數
else if(a1%2==0 && a2%2!=0){
return -1;//偶數一定是小的,排在前面
}
//a1是奇數 a2是奇數
else if(a1%2!=0 && a2%2!=0){
return a1-a2;//都是奇數默認順序排(從小到大)
}
//a1是奇數 a2是偶數
else if(a1%2!=0 && a2%2==0){
return 1;//奇數一定是大的,排在後面
}
else return 0;
}
};
set1 = new TreeSet(c);
set1.add(1);
set1.add(2);
set1.add(3);
set1.add(4);
set1.add(5);
set1.add(6);
set1.add(7);
set1.add(8);
set1.add(9);
set1.add(10);
this.showCollection(set1);
}
Map接口常用的實現類:
HashMap、TreeMap、HashTable
HashMap的聲明
public class HashMap<K,V> extends AbstractMap<K,V>
implements Map<K,V>, Cloneable, Serializable {
//...
}
Map接口中定義的方法:
void clear()
從該Map中刪除所有的映射(可選操作)。
boolean containsKey(Object key)
如果此映射包含指定鍵的映射,則返回 true 。
boolean containsValue(Object value)
如果此Map將一個或多個鍵映射到指定的值,則返回 true 。
boolean equals(Object o)
將指定的對象與此映射進行比較以獲得相等性。
V get(Object key)
返回到指定鍵所映射的值,或 null如果此映射包含該鍵的映射。
int size()
返回此Map中鍵值映射的數量。
int hashCode()
返回此Map的哈希碼值。
boolean isEmpty()
如果此Map不包含鍵值映射,則返回 true 。
Set<K> keySet()
返回此Map中包含的鍵的Set視圖。
V put(K key, V value)
將指定的值與該映射中的指定鍵相關聯(可選操作)。
Collection<V> values()
返回此Map中包含的值的Collection視圖。
Set<Map.Entry<K,V>> entrySet()
返回此Map中包含的映射的Set視圖。
void putAll(Map<? extends K,? extends V> m)
將指定Map的所有映射覆制到此映射(可選操作)。
V remove(Object key)
如果存在(從可選的操作),從該Map中刪除一個鍵的映射。
封裝遍歷Map的Key的方法
private void showMapKey(Map map){
Set set = map.keySet();
Iterator it = set.iterator();
System.out.println("\n 遍歷Map中的key開始");
while(it.hasNext()){
Object key = it.next();
System.out.println("\tkey = "+key);
}
System.out.println(" 遍歷Map中的key結束");
}
封裝遍歷Map的Value的方法
private void showMapValue(Map map){
Collection c = map.values();
Iterator it = c.iterator();
System.out.println("\n 遍歷Map中的key開始");
while(it.hasNext()){
Object value = it.next();
System.out.println("\tvalue = "+value);
}
System.out.println(" 遍歷Map中的key結束");
}
通過Map的子接口Entry遍歷鍵值對
//Map中的鍵值對
//Entry是map的一個內部接口
private void showMapEntry(Map map){
Set set = map.entrySet();
Iterator it = set.iterator();
System.out.println("\n 遍歷Map中的entry開始");
while(it.hasNext()){
Object obj = it.next();
Entry entry = (Entry) obj;
Object key = entry.getKey();
Object value = entry.getValue();
System.out.println("\tkey="+key+",value="+value);
}
System.out.println(" 遍歷Map中的entry結束");
}
通過先得到Map的key,再拿到value遍歷鍵值對
private void showMapKeyAndValue(Map map){
Set set = map.keySet();
Iterator it = set.iterator();
System.out.println("\n 遍歷Map中的key和value開始");
while(it.hasNext()){
Object key = it.next();
Object value = map.get(key);
System.out.println("\tkey="+key+",value="+value);
}
System.out.println(" 遍歷Map中的key和value結束");
}
將類對象數據存入Map並調用
public void test2(){
Map map =null;
map = new HashMap();
Student s1 = new Student(1L,"zzb1",21);
Student s2 = new Student(2L,"zzb2",22);
Student s3 = new Student(3L,"zzb3",24);
Student s4 = new Student(4L,"zzb4",20);
Student s5 = new Student(5L,"zzb5",25);
map.put(s1.getId(),s1);
map.put(s2.getId(),s2);
map.put(s3.getId(),s3);
map.put(s4.getId(),s4);
map.put(s5.getId(),s5);
System.out.println(map.get(1L));
System.out.println(map.get(2L));
System.out.println(map.get(3L));
System.out.println(map.get(4L));
}
Collections
Collections是操作集合對象的工具類,之前用的另一個工具類是Arrays。
注意,Collection和Collections的區別:前者是接口,後者是工具類
測試fill、max、min
//fill方法使用給的值,把集合中原有的數據替換掉
public void test1(){
List list = new ArrayList();
list.add(1);
list.add(2);
list.add(3);
Collections.fill(list,"hello");
this.showCollection(list);
}
//max方法 獲取集合中排序後的最大值
//min方法 獲取集合中排序後的最小值
public void test2(){
List list = new ArrayList();
list.add(10);
list.add(17);
list.add(51);
list.add(12);
list.add(31);
Object obj = Collections.max(list);
System.out.println(obj);
}
測試reverse方法
//reverse方法 反轉集合的元素的排列順序
public void test3(){
List list = new ArrayList();
list.add(10);
list.add(17);
list.add(51);
list.add(12);
list.add(31);
Collections.reverse(list);
this.showCollection(list);
}
//sort 方法可以給list集合進行排序
public void test4(){
List list = new ArrayList();
list.add(10);
list.add(17);
list.add(51);
list.add(12);
list.add(31);
//可以使用自然排序,要求list的元素實現comparable接口
//Collections.sort(list);
//同時也可以選擇使用比較器來進行排序
Collections.sort(list,new Comparator(){
public int compare(Object o1,Object o2){
int a1 = (Integer)o1;
int a2 = (Integer)o2;
return -(a1-a2);
}
});
this.showCollection(list);
}
測試swap、shuffle方法
//swap方法 指定集合倆個下標的值進行交互
public void test5(){
List list = new ArrayList();
list.add(10);
list.add(17);
list.add(51);
list.add(12);
list.add(31);
Collections.swap(list,1,3);
this.showCollection(list);
}
//shuffle方法 隨機打亂集合中的元素
public void test6(){
List list = new ArrayList();
list.add(10);
list.add(17);
list.add(51);
list.add(12);
list.add(31);
Collections.shuffle(list);
this.showCollection(list);
}
// 這些方法可以把一個非線程安全的集合變爲一個線程安全的集合
// synchronizedSet
// synchronizedMap
// synchronizedList
// synchronizedCollection
private void showCollection(Collection c){
Iterator it = c.iterator();
System.out.println(" 遍歷集合元素開始");
while(it.hasNext()){
Object obj = it.next();
System.out.println("\t"+obj);
}
System.out.println(" 遍歷集合元素結束");
}