在集合的應用開發中,集合的若干接口和若干個子類是最最常使用的,可是在JDK中提供了一種集合操做的工具類 —— Collections,能夠直接經過此類方便的操做集合。 Collections類的定義: public class Collections extends Objectjava
Collections類的經常使用方法及常量 工具
No.spa |
方法code |
類型對象 |
描述排序 |
1接口 |
public static final List EMPTY_LIST開發 |
常量it |
返回一個空的List集合io |
2 |
public static final Set EMPTY_SET |
常量 |
返回空的Set集合 |
3 |
public static final Map EMPTY_MAP |
常量 |
返回空的Map集合 |
4 |
public static <T> boolean addAll(Collection<? super T> c, T... a) |
普通 |
爲集合添加內容 |
5 |
public static <T extends Object & Comparable<? super T>> T max(Collection<? extends T> coll) |
普通 |
找到最大的內容,按比較器排序 |
6 |
public static <T extends Object & Comparable<? super T>> T min(Collection<? extends T> coll) |
普通 |
找到集合中的最小內容,按比較器排序 |
7 |
public static <T> boolean replaceAll(List<T> list,T oldVal,T newVal) |
普通 |
用新的內容替換集合的指定內容 |
8 |
public static void reverse(List<?> list) |
普通 |
集合反轉 |
9 |
public static <T> int binarySearch(List<? extends Comparable<? super T>> list,T key) |
普通 |
查找集合中的指定內容 |
10 |
public static final <T> List<T> emptyList() |
普通 |
返回一個空的List集合 |
11 |
public static final <K,V> Map<K,V> emptyMap() |
普通 |
返回一個空的Map集合 |
12 |
public static final <T> Set<T> emptySet() |
普通 |
返回一個空的Set集合 |
13 |
public static <T extends Comparable<? super T>> void sort(List<T> list) |
普通 |
集合排序操做,根據Comparable接口進行排序 |
14 |
public static void swap(List<?> list,int i,int j) |
普通 |
交換指定位置的元素 |
實例操做一:返回不可變的集合
import java.util.Collections; import java.util.List; import java.util.Set; public class CollectionsDemo01 { public static void main(String[] args) { List<String> allList = Collections.emptyList() ;// 返回不可變的空List集合 Set<String> allSet = Collections.emptySet() ; // 返回不可變的空Set集合 allList.add("Hello") ; // 錯誤,沒法加入 } }
實例操做二:爲集合增長內容
import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; import java.util.List; public class CollectionsDemo02 { public static void main(String[] args) { List<String> all = new ArrayList<String>(); // 實例化List Collections.addAll(all, "A", "B", "C"); // 增長內容 Iterator<String> iter = all.iterator() ; // 實例化Iterator對象 while (iter.hasNext()) { // 迭代輸出 System.out.print(iter.next() + "、"); // 輸出內容 } } }
實例操做三:反轉集合中的內容
import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; import java.util.List; public class CollectionsDemo03 { public static void main(String[] args) { List<String> all = new ArrayList<String>(); // 實例化List Collections.addAll(all, "A", "B", "C"); // 增長內容 Collections.reverse(all) ; // 內容反轉保存 Iterator<String> iter = all.iterator() ; // 實例化Iterator對象 while (iter.hasNext()) { // 迭代輸出 System.out.print(iter.next() + "、"); // 輸出內容 } } }
實例操做四:檢索內容
import java.util.ArrayList; import java.util.Collections; import java.util.List; public class CollectionsDemo04 { public static void main(String[] args) { List<String> all = new ArrayList<String>(); // 實例化List Collections.addAll(all, "A", "B", "C"); // 增長內容 int point = Collections.binarySearch(all, "C") ; // 檢索內容 System.out.println("檢索結果:" + point); // 輸出位置 point = Collections.binarySearch(all, "D") ; // 檢索內容,內容不存在 System.out.println("檢索結果:" + point); // 輸出位置 } }
實例操做五:替換集合中的內容
import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; import java.util.List; public class CollectionsDemo05 { public static void main(String[] args) { List<String> all = new ArrayList<String>(); // 實例化List Collections.addAll(all, "A", "B", "C"); // 增長內容 if(Collections.replaceAll(all, "A", "D")){ // 替換內容 System.out.println("內容替換成功!"); // 輸出信息 } System.out.print("替換以後的結果:") ; // 輸出信息 Iterator<String> iter = all.iterator() ; // 實例化Iterator對象 while (iter.hasNext()) { // 迭代輸出 System.out.print(iter.next() + "、"); // 輸出內容 } } }
實例操做六:集合排序
import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; import java.util.List; public class CollectionsDemo06 { public static void main(String[] args) { List<String> all = new ArrayList<String>(); // 實例化List Collections.addAll(all, "一、A", "二、B", "五、M"); // 增長內容 Collections.addAll(all, "三、X"); // 增長內容 Collections.addAll(all, "四、Z"); // 增長內容 System.out.print("排序以前的集合:"); // 輸出信息 Iterator<String> iter = all.iterator() ; // 實例化Iterator對象 while (iter.hasNext()) { // 迭代輸出 System.out.print(iter.next() + "、"); // 輸出內容 } Collections.sort(all) ; // 集合排序 System.out.print("\n排序以後的集合:") ; // 輸出信息 iter = all.iterator() ; // 實例化Iterator對象 while (iter.hasNext()) { // 迭代輸出 System.out.print(iter.next() + "、"); // 輸出內容 } } }
實例操做七:交換指定位置的內容
import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; import java.util.List; public class CollectionsDemo07 { public static void main(String[] args) { List<String> all = new ArrayList<String>(); // 實例化List Collections.addAll(all, "一、A", "二、B", "三、C"); // 增長內容 System.out.print("交換以前的集合:") ; // 輸出信息 Iterator<String> iter = all.iterator() ; // 實例化Iterator對象 while (iter.hasNext()) { // 迭代輸出 System.out.print(iter.next() + "、"); // 輸出內容 } Collections.swap(all,0,2) ; // 交換指定位置的內容 System.out.print("\n交換以後的集合:") ; // 輸出信息 iter = all.iterator() ; // 實例化Iterator對象 while (iter.hasNext()) { // 迭代輸出 System.out.print(iter.next() + "、"); // 輸出內容 } } }