在集合的应用开发中,集合的若干接口和若干个子类是最最常使用的,可是在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() + "、"); // 输出内容 } } }