java集合类详细概述
生活随笔
收集整理的這篇文章主要介紹了
java集合类详细概述
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
2019獨角獸企業重金招聘Python工程師標準>>>
Java集合類? 集合中存放的是對象的引用,而非對象本身 ,出于表達上的便利,簡稱為"集合中的對象".
? Set(集):集合中的對象不按特定方式排列,并且沒有重復對象,它的有些實現類能對集合中的對象按特定方式排列.
? ? ?set接口主要有兩個實現類HashSet和TreeSet,HashSet類按照哈希算法來存取集合中的對象,存取速度比較快,HashSet類還有一個子類LinkedHashSet類,不僅
? ? ?實現了哈希算法,而且實現了鏈表數據結構,TreeSet類實現了SortedSet接口,具有排序功能.
? ? ?那么,當一個新的對象加入到Set集合中,Set的add()方法是如何判斷這個對象是否已經存在于集合中的呢?
? ? ? boolean isExists=false;
? ? ? Iterator it=set.iterator();
? ? ? while(it.hasNext())
? ? ? {
? ? ? ? ? ?Object oldObject=it.next();
? ? ? ? ? ?if(newObject.equals(oldObject))
? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? isExists=true;
? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? }
? ? ? }
? ? ? 可見,Set采用對象的equals()方法比較兩個對象是否相等,而不是采用"=="比較運算符,以下程序代碼盡管兩次調用了Set的add()方法,
? ? ? 實際上只加入了一個對象:
? ? ? Set set=new HashSet();
? ? ? String s1=new String("hello");
? ? ? String s2=new String("hello");
? ? ? ?set.add(s1);
? ? ? ?set.add(s2);
? ? ? 雖然變量s1和s2實際上引用的是兩個內存地址不同的字符串對象,但是由于s2.equals(s1)的比較結果為true,因此Set認為他們是相等的對象,當第二次調用
? ? ? Set的add()方法時,add()方法不會把s2引用的字符串對象加入到集合中.
? ?HashSet類
? ? 按照哈希算法來存取集合中的對象,具有很好的存取性能,當HashSet向集合中加入一個對象時,會調用對象的hashCode()方法獲得哈希碼,然后根據這個哈希碼
? ? 進一步計算出對象在集合中的存放位置.
? ? 在Object類中定義了hashCode()和equals()方法,Object類的euqals()方法按照內存地址比較對象是否相等,因此如果object1.equals(object2)為true,表明object1
? ? 變量和object2變量十九上引用同一個對象.那么object1和object2的哈希碼也應該相同.
? ? ***如果用戶定義的類覆蓋了Object類的equals()方法,但是沒有覆蓋Object類的hashCode()方法,就會導致當object1.equals(object2)為true時,而object1和object2的
? ? ? ?哈希碼不一定一樣,這樣使HashSet無法正常工作.
? ?TreeSet類:
? ? ? ?實現了SortedSet接口,能夠對集合中的對象進行排序.
? ? ? ?如:
? ? ? ? ?Set set=new TreeSet();
? ? ? ? ? set.add(new Integer(7));
? ? ? ? ? set.add(new Integer(9));
? ? ? ? ? set.add(new Integer(8));
? ? ? ? ?Iterator it=set.iterator();
? ? ? ? ? while(it.hasNext())
? ? ? ? ? {
? ? ? ? ? ? ? ?System.out.println(it.next());
? ? ? ? ? }
? ? ? ? ?輸出結果為:6 7 8
? ? ? 當TreeSet向集合中加入一個對象時,會把它插入到有序的對象序列中,那么TreeSet是如何對對象進行排序的捏?TreeSet支持
? ? ? 兩種排序方式:自然排序和客戶化排序,默認情況下是自然排序.
? ? ? 在JDK中,有一部分類實現了Comparable接口,如Integer,Double和String等,Comparable接口有一個compareTo(Object o)方法,
? ? ? 它返回整數類型,對于表達式x.compareTo(y),如果返回值為0,表示x和y相等,如果返回值大于0,表示x大于y,如果小于0,表示x<y.
? ? ? ?TreeSet調用對象的compareTo()方法比較集合中對象的大小,然后進行升序排序,這種方式稱為自然排序.
? ?客戶化排序:
? ? ? ?java.util.Comparator接口用于指定具體的排序方式,它有個compare(Object obj1,Object obj2),用于比較兩個對象的大小.
? ? ? ?當表達式compare(x,y)的值大于0,表示x大于y,小于0,表示x小于y,等于0,表示x等于y,如果想讓TreeSet進按照Customer對象的
? ? ? ?name屬性進行降序排列,可以先創建實現Comparator接口的類CustomerComparator,如:
? ? ? ?import java.util.*;
? ? ? ?public class CustomerComparator implements Comparator
? ? ? ? {
? ? ? ? ? ? ? public int compare(Object o1,Object o2)
? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ?Customer c1=(Custoemr)o1;
? ? ? ? ? ? ? ? ? ? ?Customer c2=(Customer)o2;
? ? ? ? ? ? ? ? ? ? if(c1.getName().compareTo(c2.getName())>0) return -1;
? ? ? ? ? ? ? ? ? ? if(c1.getName().compareTo(c2.getName())<0) return 1;
? ? ? ? ? ? ? ? ? ? return 0;
? ? ? ? ? ? ? }?
? ? ? ? }
? ? ? ? 接下來在構造TreeSet的實例時,調用它的TreeSet(Comparator comparator)構造方法
? ? ? ? ? ? ?Set set=new TreeSet(new CustomerComparator());
? ? ? ? ? ? ? ?Customer c1=new Customer("TOM",15);
? ? ? ? ? ? ? ?Customer c2=new Customer("JACK",20);
? ? ? ? ? ? ? ?Customer c3=new Customer("MIKE",38);
? ? ? ? ? ? ? ?set.add(c1);set.add(c2);set.add(c3);
? ? ? ? ? ? ? Iterator it=set.iterator();
? ? ? ? ? ? ? ?while(it.hasNext())?
? ? ? ? ? ? ? ?{Custoemr customer=(Customer)it.next();
? ? ? ? ? ? ? ? ? System.out.println(customer.getName()+"" +customer.getAge();)
? ? ? ? ? ? ? ?}
? ? ? ? ?當TreeSet向集合中加入Customer對象時,會調用CustomerComparator類的compare()方法進行排序,以上Tree按照
? ? ? ? ?Custoemr對象的name屬性進行降序排列,最后輸出為:
? ? ? ? ?TOM 15 ? ?MIKE 38 JACK 16
? ?
? List(列表):對象以線性方式存儲,集合中的對象按索引位置排序,可以有重復對象,允許按照對象在集合中的索引位置檢索對象.
? ? ? ? ? 實現類有LinkedList,ArrayList和Vector,LinkedList采用鏈表數據結構,而ArrayList代表大小可變的數組, Vector和
? ? ? ? ? ArrayList比較相似,兩者的區別在于Vecotr類的實現采用了同步機制,而ArrayList沒有使用同步機制/
? ? ? ? ? List按索引排列:
? ? ? ? ? ? ? List list=new ArrayList();
? ? ? ? ? ? ? list.add(new Integer(3));
? ? ? ? ? ? ? list.add(new Integer(4));
? ? ? ? ? ? ? list.add(new Integer(3));
? ? ? ? ? ? ? list.add(new Integer(2));
? ? ? ? ? ? ? List的get(int index)方法返回集合中由參數index指定的索引位置的對象,第一個加入到集合中的對象的索引位置為0,
? ? ? ? ? ? ? for( int i=0,i<list.size;i++)
? ? ? ? ? ? ? ? {System.out.println(list.get(i));}
? ? ? ? ? ? ? 輸出結果為:3 4 3 2.
? ? ? ? ? List只能對集合中的對象按索引位置排序,如果希望對List中的對象按其他特定方式排序,可以借助Comparator接口和Collections類.
? ? ? ? ? Collections類是Java集合API中的輔助類,它提供了操縱集合的各種靜態方法,其中sort()方法用于對List中的對象進行排序:
? ? ? ? ? sort(List list):對List中的對象進行自然排序.
? ? ? ? ? sort(List list,Comparator comparator):對List中的對象進行客戶化排序,comparator參數指定排序方式.
? ? ? ? ? 對以下List進行自然排序:
? ? ? ? ? List list=new ArrayList();
? ? ? ? ? list.add(new Integer(3));
? ? ? ? ? list.add(new Integer(4));
? ? ? ? ? list.add(new Integer(3));
? ? ? ? ? list.add(new Integer(2));
? ? ? ? ? Collections.sort(list);
? ? ? ? ? for(int i=0;i<list.size();i++)
? ? ? ? ? {
? ? ? ? ? ? ? ? ?System.out.println(list.get(i));
? ? ? ? ? }
? ? ? ? ? 以上輸出結果:2 3 3 4
? Map(映射):集合中的每一個元素包含一對鍵對象和一對值對象,集合中沒有重復的鍵對象,值對象可以重復,它的有寫實現類能
? 對集合中的鍵對象進行排序.
? Map map=new HashMap();
? map.put("1","Mon");
? map.put("1",Monday);
? map.put("2","monday");
? 由于第一次和第二次加入到Map中的鍵對象都是1,所以第一次加入的值對象將被覆蓋,而第二個和第三個的值對象雖然相同,但是
? 鍵對象不一樣,所以分配了不同的地址空間,所以不會覆蓋,也就是說一共有兩個元素在Map集合中.
??
? Map有兩種比較常用的實現:HashMap和TreeMap.Hashmap按照哈希算法來存取鍵對象,有很好的存取能力,為了保證HashMap能正常工作,
? 和HashSet一樣,要求當兩個鍵對象通過equals()方法比較為true時,這兩個鍵對象的hashCode()方法返回的哈希碼也一樣.
? TreeMap實現了SortedMap接口,能對鍵對象進行排序,和TreeSet一樣,TreeMap也支持自然排序和客戶化排序兩種方式,以下程序中的TreeMap
? 會對四個字符串類型的鍵對象"1","3","4","2"進行自然排序:
? Map map=new TreeMap();
? map.put("1","Monday");
? map.put("3","Wendsday");
? map.put("4","Thursday");
? map.put("2","Tuesday");
? //返回集合中所有鍵對象的集合
? Set keys=map.keySet();
? Iterator it=keys.iterator();
? while(it.hasNext)
? {String key=(String)it.next();
? ?//根據鍵對象得到值對象
? ?String value=(String)map.get(key);
? ?System.out.println(key+"" +value);
? }
? 以上輸出結果為:1 Monday ?2 Wendsday 3 Thursday 4 Tuesday
?
? 如果希望TreeMap進行客戶化排序,可以調用它的另一個構造方法TreeMap(Comparator comparator),參數comparator指定具體的排序方式.
轉載于:https://my.oschina.net/rouchongzi/blog/123028
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的java集合类详细概述的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: could not insert int
- 下一篇: 【编译打包】haproxy 1.4.23