操作集合的工具类 collections

来源:互联网 发布:c语言elseif什么意思 编辑:程序博客网 时间:2024/06/05 15:35
   JAVA提供了一个操作Set,List和Map等集合的工具类:Collections,该工具类里提供了大量方法对集合元素进行排序,查询和修改等操作,还提供了将集合对象设置为不可变,对集合对象实现同步控制等方法。

 

》排序操作

 

下面代码将对List集合进行排序

 

import java.util.*;

public class SortTest

{

       public static void main(String[] args)

       {

                ArrayList nums = new ArrayList();

                nums.add(2);

                nums.add(-5);

                nums.add(3);

                 nums.add(0);

 

                //输出[上面元素]

                System.out.println(nums);

 

                 //将List集合元素的次序反转

                 Collections.reverse(nums);

                 //输出反转后的元素

                  System.out.println(nums);

 

                  //将List集合元素按自然顺序排序

                  Collections.sort(nums);

                   //输出[-5,0,2,3]

                   System.out.println(nums);

 

                  //将List集合元素按随机顺序排序

                   Collection.shuffle(nums);

                  //每次输出不一样

                   System.out.println(nums);

        }

}

 

编译结果:

 

 

 

》查找,替换操作

 

public class TestSearch

{

       public static void main(String[] args)

       {

                  ArrayList nums =new ArrayList();

                  nums.add(2);

                  nums.add(-5);

                  nums.add(3);

                  nums.add(0);

                  //输出[2,-5,3,0]

                  System.out.println(nums);

 

                   //输出最大元素

                   System.out.println(Collections.max(nums));

 

                   //输出最小元素

                   System.out.println(Collections.min(nums));

 

                   //将nums中的0使用1来代替

                   Collections.replaceAll(nums,0,1);

                   //输出[2,-5,3,1] 

                   System.out.println(nums);

 

                    //判断-5在List集合中出现的次数,返回1

                    System.out.println(Collections.frequency(nums,-5));

 

                   //对nums集合排序,并输出[-5,1,2,3]

                    Collections.sort(nums);

                    System.out.prntln(nums);

 

                     //只有排序后的List集合才可用二分法查询

                     System.out.println(Collections.binarySearch(nums,3)) ;                     

 

        }

}

 

编译结果:

 

 

 

 

 》集合同步控制

 

public class TestSynchronized

{

        public static void main(String[] args) 

        {

                 //下面程序创建了四个同步的集合对象

                 Collection c = Collections.synchronizedCollectins(new ArrayList());

                 List list = Collections.synchronizedList(new ArrayList());

                 Set s = Collections.syncronizedSet(new HashSet());

                 Map m = Collections.syncronizedMap(new HashMap());

         }

}

 

程序直接将新创建的集合对象传给了Collections的 synchronized???方法,这样就直接获得 List, Set ,Map 的线程安全实现版本

 

 

》设置不可变集合

 

public class TestUnmodifiable

{

         public static void main(String[] args)

         {

                     //创建一个空的,不可改变的List对象

                    List unmodifiableList = Collections.emptyList();

 

                   //创建一个只有一个元素,且不可改变的Set对象

                   Set unmodifiableSet = Collections.singleton("世界您好");

 

                   //创建一个普通Map对象

                   Map scores = new HashMap();

                    scores.put("语文",80);

                    scores.put("java",82);

 

                  //返回普通Map对象对应的不可变版本

                   Map unmodifiableMap = Collections.unmodifiableMap(scores);

 

                    //下面代码都将引发UnsupportedOperationException异常

                    unmodifiableList.add("测试元素");

                     unmodifiableSet.add("测试元素");

                    unmodifiableMap.put("语文",90);

         }

}

 

 

 

不可变集合对象只能访问集合元素,不可修改集合元素。

 

原创粉丝点击