(一)Collection
|--Collection(接口)
|--List(接口):有序,可重複(有角標),特有迭代器ListIterator,允許多個null
|--ArrayList(類):不同步,查詢快,增刪稍慢,底層是數組結構
|--LinkedList(類):不同步,查詢稍慢,增刪快,底層是鏈表結構
|--Vector(類):同步,效率低,底層是數組結構,已被ArrayList取代
|--Set(接口):無序,不可重複,沒有索引
|--HashSet(類):底層是hash表,不同步,通過hashCode和equals保證唯一
|--TreeSet(類):底層二叉樹,不同步,使用自然順序(默認)或比較器進行排序
(二)Map
|--Map(接口)
|--Hashtable(類):底層是Hash表結構,不可以存入null鍵值。同步,.效率低
。
|--HashMap:底層是Hash表結構,允許用 null 值鍵,不同步,替代HashTable效率高。
|--TreeMap:底層是二叉樹結構。不同步。可以用於給map集合中的鍵進行排序。
(ps:Set底層就是用來Map結構中的Key)
(三)工具類Collections
由於Collections中全部爲靜態方法,因此不用實現對象,可以通過Collections.方法名()來使用。
常用方法:
(1) sort:對List排序,可以自然排序,也可傳入比較器。
(2) max/min:獲得collection的最大/小值,可指定比較器
(3) BinarySearch:二分查找,可指定比較器
(4) Fill:用指定元素替換List中所有元素
(5) replaceAll:使用指定值替換List中所有某一指定值
(6) reverse:翻轉指定List中的所有順序
(7) reverseOrder:返回一個比較器,強行逆轉自然順序
(8) shuffle:使用默認隨機源對List順序進行置換
(9) synchronizedSet/ synchronizedMap/synchronizedList/ synchronizedCollection:
返回一個同步的(線程安全的)Set/Map/List/Collection
(四)工具類Arrays
Arrays中同樣全爲靜態方法,主要用於對數組進行各種操作(搜索或排序)。
常用方法:
(1) BinarySearch:二分查找,可查找任意類型數組,可指定比較器,可指定比較範圍。
(2) hashCode:基於指定數組內容返回哈希碼
(3) sort:升序排序,可指定範圍,也可指定比較器
(五)必要練習
1、對“shemsasls,fkfhlskf.fdhg:hah”字符串進行操作,獲取該字符串中字母出現的次數
打印格式:a(4)b(1)......
2、對學生對象的年齡進行升序排序,因爲數據是以鍵值對的形式存在,所以要使用可以排序的Map集合——TreeMap
練習答案:
1、
/*
需求:對“shemsasls,fkfhlskf.fdhg:hah”字符串進行操作,獲取該字符串中字母出現的次數
打印格式:a(4)b(1)......
*/
import java.util.*;
class MapTest
{
publicstatic void main(String[] args)
{
Stringstr = "shemsasls,fkfhlskf.fdhg:hah";
TreeMap<Character,Integer>tm = new TreeMap<Character,Integer>();
char[]ch = str.toCharArray();
intnum = 0;
for(int i = 0; i < str.length(); i++ )
{
if(!(ch[i]>='a' && ch[i]<='z' || ch[i]>='A' &&ch[i]<='Z'))
{
continue;
}
Integervalue = tm.get(ch[i]);
if(value!= null)
{
num= value;
}
tm.put(ch[i],++num);
num= 0;
}
Set<Map.Entry<Character,Integer>>entrySet = tm.entrySet();
Iterator<Map.Entry<Character,Integer>>it = entrySet.iterator();
while(it.hasNext())
{
Map.Entry<Character,Integer>me = it.next();
Characterkey = me.getKey();
Integervalue = me.getValue();
System.out.print(key+"("+value+")");
}
}
}
2、
import java.util.*;
import static java.lang.System.*;
/*
需求:對學生對象的年齡進行升序排序,因爲數據是以鍵值對的形式存在,所以要使用可以排序的Map集合——TreeMap
*/
class TreeMapDemo
{
publicstatic void main(String[] args)
{
TreeMap<Student,String>tm = new TreeMap<Student,String>(new MyComparator());
tm.put(newStudent("zhangsan3",23),"nanjin");
tm.put(newStudent("zhangsan2",22),"shanghai");
tm.put(newStudent("zhangsan4",22),"tianjin");
tm.put(newStudent("zhangsan1",21),"beijin");
/*
Map遍歷有兩種方法,setKey()和Map.entrySet(),他們都可以遍歷Map,但是在速度上有差別。
setKey()的速度較慢,因爲他實際上遍歷了兩遍,第一遍的時候取出key,第二遍的時候通過key的set集合取出所有value。
Map.entrySet()速度較快,因爲它只遍歷了一遍。
所以推薦使用第二種方法。
*/
//使用第二種遍歷方法
Set<Map.Entry<Student,String>>entrySet = tm.entrySet();
Iterator<Map.Entry<Student,String>>it = entrySet.iterator();
while(it.hasNext())
{
Map.Entry<Student,String>me = it.next();
out.println(me.getKey()+"_"+me.getValue());
}
}
}
//定義學生類
class Student implements Comparable
{
privateString name;
privateint age;
Student(Stringname,int age)
{
this.name= name;
this.age= age;
}
publicString getName()
{
returnthis.name;
}
publicint getAge()
{
returnthis.age;
}
publicint compareTo(Object obj)
{
if(!(obj instanceof Student))
{
thrownew ClassCastException("不是學生類");
}
Studentst = (Student)obj;
intnum = this.age-st.getAge();
if(num == 0)
{
returnthis.name.compareTo(st.getName());
}
returnnum;
}
publicString toString()
{
return this.name+","+this.age;
}
}
/*
Comparable和Comparator的區別:
1.Compatable是通用的接口,用戶可以通過實現它的compareTo()方法來完成自己的特定比較。
而Comparator可以看成一種算法的實現,在容器集合collection需要比較功能的時候,來指定這個比較器,這可以看成一種設計模式(策略模式),將算法和設計分離。
2.前者比較固定,和一個具體類相綁定,而後者比較靈活,它可以被用於各個需要比較功能的類使用。
可以說前者屬於靜態綁定,而後者屬於動態綁定。
3.一個類實現了Comparable接口標明這個類的對象之間是可以互相比較的。如果用數學語言描述的話就是這個類的對象組成的集合中存在一個全序。
這樣,這個類對象組成的集合就可以使.用.Sort.方.法.排序了.
4.而Comparato的作用有兩個:
a.如果類的設計師沒有考慮到Compare的問題而沒有實現Comparable接口,可以通過Comparator來實現比較算法進行排序。
b.可以更加靈活的實現排序規則,爲了使用不同的排序標準做準備,比如升序,降序,或者將來想通過類的其他字段進行排序。
*/
//自定義比較器,使年齡按照倒序排列
class MyComparator implementsComparator<Student>
{
publicint compare(Student st1,Student st2)
{
intnum = st2.getAge()-st1.getAge();
if(num == 0)
{
returnst2.getName().compareTo(st1.getName());
}
returnnum;
}
}