collection與collections的關系?
public class Collectionsextends Object
collection與collections沒有直接的關系,但是與集合的各個接口都有操作的方法支持。
1,驗證空集合的操作。
static <T> List<T> emptyList() 返回空的列表(不可變的)。
代碼:
package 類集; import java.util.Collections; import java.util.List; import java.util.Set; public class test1{ public static void main(String args[]){ List<String> allList = Collections.emptyList() ; // 返回空的 List集合 Set<String> allSet = Collections.emptySet() ; // 返回空的 List集合 allList.add("Hello") ; // 加入數據 } };
操作結果:
Exception in thread "main" java.lang.UnsupportedOperationException at java.util.AbstractList.add(AbstractList.java:148) at java.util.AbstractList.add(AbstractList.java:108) at 類集.test1.main(test1.java:9)
發現此時添加不了數據。
collections提供了專門的 添加操作。
static <T> boolean addAll (Collection<? super T> c, T... elements) 將所有指定元素添加到指定 collection 中。
使用了可變數量的參數,可以輸入各種類型的數據。
package 類集; import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; import java.util.List; public class test1{ public static void main(String args[]){ List<String> all = new ArrayList<String>() ; Collections.addAll(all,"MLDN","LXH","mldnjava") ; //給 ALL 加入各種類型數據 Iterator<String> iter = all.iterator() ; while(iter.hasNext()){ System.out.print(iter.next() + "、") ; } } };
輸出結果:
MLDN、LXH、mldnjava、
反轉操作:
static void reverse(List<?> list) 反轉指定列表中元素的順序。
代碼:
package 類集; import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; import java.util.List; public class test1{ public static void main(String args[]){ List<String> all = new ArrayList<String>() ; // 返回空的 List集合 Collections.addAll(all,"MLDN","LXH","mldnjava") ; Collections.reverse(all) ; // 內容反轉 Iterator<String> iter = all.iterator() ; while(iter.hasNext()){ System.out.print(iter.next() + "、") ; } } };
二分檢索操作
static <T> int binarySearch(List<? extends T> list, T key, Comparator<? super T> c) 使用二分搜索法搜索指定列表,以獲得指定對象。
返回的結果是位置信息。
代碼:
package 類集; import java.util.ArrayList; import java.util.Collections; import java.util.List; public class test1{ public static void main(String args[]){ List<String> all = new ArrayList<String>() ; // 返回空的 List集合 Collections.addAll(all,"MLDN","LXH","mldnjava") ; int point = Collections.binarySearch(all,"LXH") ; // 檢索數據 System.out.println("檢索結果:" + point) ; } };
結果:
檢索結果:1
替換內容操作
static <T> boolean replaceAll(List<T> list, T oldVal, T newVal) 使用另一個值替換列表中出現的所有某一指定值。
返回結果是bool類型,所以可以用在if判斷語句中來判斷是否替換成功。
代碼:
package 類集; import java.util.ArrayList; import java.util.Collections; import java.util.List; public class test1{ public static void main(String args[]){ List<String> all = new ArrayList<String>() ; // 返回空的 List集合 Collections.addAll(all,"MLDN","LXH","mldnjava") ; if(Collections.replaceAll(all,"LXH","李興華")){// 替換內容 System.out.println("內容替換成功!") ; } System.out.print("替換之后的結果:") ; System.out.print(all) ; } };
操作結果:
內容替換成功!
替換之后的結果:[MLDN, 李興華, mldnjava]
內容排序操作
static <T extends Comparable<? super T>> void sort(List<T> list) 根據元素的自然順序 對指定列表按升序進行排序。
代碼:
package 類集; import java.util.ArrayList; import java.util.Collections; import java.util.List; public class test1{ public static void main(String args[]){ List<String> all = new ArrayList<String>() ; // 返回空的 List集合 Collections.addAll(all,"1、MLDN","3、mldnjava","2、LXH") ; Collections.addAll(all,"B、www.mldn.cn") ; Collections.addAll(all,"A、www.mldnjava.cn") ; System.out.println("排序之前的集合:" + all) ; Collections.sort(all) ; System.out.println("排序之后的集合:" + all) ; } };
返回結果:
排序之前的集合:[1、MLDN, 3、mldnjava, 2、LXH, B、www.mldn.cn, A、www.mldnjava.cn]
排序之后的集合:[1、MLDN, 2、LXH, 3、mldnjava, A、www.mldnjava.cn, B、www.mldn.cn]
內容交換操作:
static void swap(List<?> list, int i, int j) 在指定列表的指定位置處交換元素。
根據交換的內容的位置進行交換。
代碼:
package 類集; import java.util.ArrayList; import java.util.Collections; import java.util.List; public class test1{ public static void main(String args[]){ List<String> all = new ArrayList<String>() ; // 返回空的 List集合 Collections.addAll(all,"1、MLDN","2、LXH","3、mldnjava") ; System.out.println("交換之前的集合:" + all) ; Collections.swap(all,0,2) ; System.out.println("交換之后的集合:" + all) ; } };
結果:
交換之前的集合:[1、MLDN, 2、LXH, 3、mldnjava]
交換之后的集合:[3、mldnjava, 2、LXH, 1、MLDN]
總結:
為了方便類集的操作,提供了collections類。與collection沒有直接關系。
只是對所有的集合接口有所支持而已。