<1>实例操作一:返回不可变的集合
Collections类中可以返回空的List、Set、Map集合,但是通过这种方式返回的对象是无法进行增加数据的,因为在这些操作中并没有实现add()方法
import java.util.Collections; import java.util.List; import java.util.Set; //================================================= // File Name : Collection_demo //------------------------------------------------------------------------------ // Author : Common //主类 //Function : Collection_demo; public class Collection_demo { public static void main(String[] args) { // TODO 自动生成的方法存根 List<String> allList = Collections.emptyList(); //返回不可变的空List集合 Set<String> allSet = Collections.emptySet(); //返回不可变的空List集合 } }
<2>实例操作二:为集合增加内容
import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; //================================================= // File Name : Collection_demo //------------------------------------------------------------------------------ // Author : Common //主类 //Function : Collection_demo; public class Collection_demo { public static void main(String[] args) { // TODO 自动生成的方法存根 List<String> all = new ArrayList<String>(); //实例化List Collections.addAll(all, "zhangsan","lisi","wangwu"); //增加内容 Iterator<String> iter = all.iterator(); //实例化iterator对象 while(iter.hasNext()){ System.out.println(iter.next()+"、"); } } }
<3>实例操作三:反转集合中的内容
import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; //================================================= // File Name : Collection_demo //------------------------------------------------------------------------------ // Author : Common //主类 //Function : Collection_demo; public class Collection_demo { public static void main(String[] args) { // TODO 自动生成的方法存根 List<String> all = new ArrayList<String>(); //实例化List Collections.addAll(all, "zhangsan","lisi","wangwu"); //增加内容 Collections.reverse(all); //内容反转保存 Iterator<String> iter = all.iterator(); //实例化iterator对象 while(iter.hasNext()){ System.out.println(iter.next()+"、"); } } }
<4>实例操作四:检索内容
import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; //================================================= // File Name : Collection_demo //------------------------------------------------------------------------------ // Author : Common //主类 //Function : Collection_demo; public class Collection_demo { public static void main(String[] args) { // TODO 自动生成的方法存根 List<String> all = new ArrayList<String>(); //实例化List Collections.addAll(all, "zhangsan","lisi","wangwu"); //增加内容 Collections.reverse(all); //内容反转保存 Iterator<String> iter = all.iterator(); //实例化iterator对象 while(iter.hasNext()){ System.out.println(iter.next()+"、"); } int point = Collections.binarySearch(all,"zhangsan"); System.out.println("检索结果:"+point); //输出位置 } }
<5>实例操作五:替换集合中的内容
import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; //================================================= // File Name : Collection_demo //------------------------------------------------------------------------------ // Author : Common //主类 //Function : Collection_demo; public class Collection_demo { public static void main(String[] args) { // TODO 自动生成的方法存根 // List<String> allList = Collections.emptyList(); //返回不可变的空List集合 // Set<String> allSet = Collections.emptySet(); //返回不可变的空List集合 List<String> all = new ArrayList<String>(); //实例化List Collections.addAll(all, "zhangsan","lisi","wangwu"); //增加内容 Collections.replaceAll(all, "wangwu", "lisi"); //替换内容 Collections.reverse(all); //内容反转保存 Iterator<String> iter = all.iterator(); //实例化iterator对象 while(iter.hasNext()){ System.out.println(iter.next()+"、"); } int point = Collections.binarySearch(all,"zhangsan"); System.out.println("检索结果:"+point); //输出位置 } }
<6>实例操作六:集合排序
import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; //================================================= // File Name : Collection_demo //------------------------------------------------------------------------------ // Author : Common //主类 //Function : Collection_demo; public class Collection_demo { public static void main(String[] args) { // TODO 自动生成的方法存根 // List<String> allList = Collections.emptyList(); //返回不可变的空List集合 // Set<String> allSet = Collections.emptySet(); //返回不可变的空List集合 List<String> all = new ArrayList<String>(); //实例化List Collections.addAll(all, "zhangsan","lisi","wangwu"); //增加内容 // Collections.replaceAll(all, "wangwu", "lisi"); //替换内容 // Collections.reverse(all); //内容反转保存 Iterator<String> iter = all.iterator(); //实例化iterator对象 while(iter.hasNext()){ System.out.println(iter.next()+"、"); } Collections.sort(all); System.out.println("排序之后的集合"); iter = all.iterator(); while(iter.hasNext()){ System.out.println(iter.next()+"、"); } int point = Collections.binarySearch(all,"zhangsan"); System.out.println("检索结果:"+point); //输出位置 } }
<7>实例操作七:交换指定位置的内容
直接使用swap()方法可以把集合中两个位置的内容进行交换
import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; //================================================= // File Name : Collection_demo //------------------------------------------------------------------------------ // Author : Common //主类 //Function : Collection_demo; public class Collection_demo { public static void main(String[] args) { // TODO 自动生成的方法存根 // List<String> allList = Collections.emptyList(); //返回不可变的空List集合 // Set<String> allSet = Collections.emptySet(); //返回不可变的空List集合 List<String> all = new ArrayList<String>(); //实例化List Collections.addAll(all, "zhangsan","lisi","wangwu"); //增加内容 // Collections.replaceAll(all, "wangwu", "lisi"); //替换内容 // Collections.reverse(all); //内容反转保存 Iterator<String> iter = all.iterator(); //实例化iterator对象 while(iter.hasNext()){ System.out.println(iter.next()+"、"); } // Collections.sort(all); Collections.swap(all,0,2); //交换指定位置的内容 System.out.println("排序之后的集合"); iter = all.iterator(); while(iter.hasNext()){ System.out.println(iter.next()+"、"); } int point = Collections.binarySearch(all,"zhangsan"); System.out.println("检索结果:"+point); //输出位置 } }