• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
风吹花落泪如雨
博客园    首页    新随笔    联系   管理    订阅  订阅

JAVA集合(五)操作集合的工具类:Collections

Java提供了一个操作Set、LIst和Map等集合的工具类:Collections,该工具提供了大量方法对集合元素进行排序、查询和修改等操作,还提供了将集合对象设置为不可变、对集合对象实现同步控制等方法。

1.排序操作

import java.util.ArrayList;
import java.util.Collections;
public class a{
  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);
    Collections.reverse(nums);//将List集合元素的次序反转
    System.out.println(nums);
    Collections.sort(nums);//自然排序
    System.out.println(nums);    
    Collections.shuffle(nums);//将List集合元素按随机顺序排序
    System.out.println(nums);
  }
}

 

2.查找、替换操作

import java.util.ArrayList;
import java.util.Collections;
public class a{
  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);
    System.out.println(Collections.max(nums));//输出最大元素,3
    System.out.println(Collections.min(nums));//输出最小元素,-5
    Collections.replaceAll(nums,0,1);//将nums中的0使用1代替
    System.out.println(nums);
    System.out.println(Collections.frequency(nums,-5));//判断-5出现的次数
    //只有排序后的List集合才可用二分法查询,输出3
    Collections.sort(nums); 
    System.out.println(Collections.binarySearch(nums,-5));
  }
}

3.同步控制

Java集合中常用的集合框架中的实现类HashSet、TreeSet、ArrayList、ArrayDeque、LinkedList、HashMap和TreeMap都是线程不安全的。
Collections提供了多个类方法可以把它们包装成线程同步的集合:

import java.util.*;
public class a{
  public static void main(String[] args){
    //下面程序创建了4个线程安全的集合对象
    Collection c = Collections.synchronizedCollection(new ArrayList());
    List list = Collections.synchronizedList(new ArrayList());
    Set s = Collections.synchronizedSet(new HashSet());
    Map m = Collections.synchronizedMap(new HashMap());
  }
}

上面程序中,直接将新创建的集合对象传给了Collections的synchronizedXxx方法,这样就可以直接获取List、Set和Map的线程安全版本

4.设置不可变集合

Collections提供了三类方法来返回一个不可变集合:
emptyXxx():返回一个空的、不可变集合对象,此处的集合可以是List、Set、SortedSet、Map、SortedMap等。
singletonXxx():返回一个只包含指定对象(只有一个或一项元素)的、不可变的集合对象,此处集合既可以是List,还可以是Map。
unmodifiableXxx():返回指定集合对象的不可变视图。此处的集合可以是List、Set、SortedSet、Map、SortedMap等。

import java.util.*;
public class a{
  public static void main(String[] args){
    //创建一个空的,不可改变的List对象
    List unmodifiableList = Collections.emptyList();
    //创建一个只有一个元素,不可改变的Set对象
    Set unmodifiableSet = Collections.singleton("疯狂Java讲义");
    Map scores = new HashMap();
    scores.put("语文",100);
    scores.put("数学",55);
    //返回普通的Map对象对应的不可变版本
    Map unmodifiableMap = Collections.unmodifiableMap(scores);
    //下面任一行代码都将引发UnsupportedOperationException异常
    unmodifiableList.add("测试元素1");
    unmodifiableSet.add("测试元素2");
    unmodifiableMap.put("测试元素3",22);
  }
}

Java9新增的不可变集合

Java9可以直接调用Set、List、Map的of()方法即可创建包含N个元素的不可变集合,这一行代码即可创建包含N个元素的集合。不可变意味着不可向集合中添加元素,也不能从集合中删除元素。

import java.util.*;
public class a{
  public static void main(String[] args){
    Set set = Set.of("Java","PHP","Web");
    System.out.println(set);
    List list = List.of("Java","PHP","Web");
    System.out.println(set);

    Map map = Map.of("语文",99,"数学",66,"英语",55);
    Map map2 = Map.ofEntries(Map.entry("语文",99),Map.entry("数学",66),Map.entry("英语",55));
     System.out.println(map);
     System.out.println(map2);
  }
}

 

 

 

 

posted @ 2018-06-13 23:32  风吹花落泪如雨  阅读(399)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3