Collections工具类(集合框架)

Collections是集合框架的工具类。里面的方法都是静态的。

1,Collections中常用方法

(1)static <T extends Comparable<? super T>> void sort(List<T> list):根据元素的自然顺序 对指定列表按升序进行排序。

static <T> void sort(List<T> list, Comparator<? super T> c):根据指定比较器产生的顺序对指定列表进行排序。

运用示例:

import java.util.*;

class StrLenComparator implements Comparator<String>{//自定义一个比较器
 public int compare(String s1,String s2){
  if(s1.length()==s2.length())
   return s1.compareTo(s2);  
  return s1.length()-s2.length();
 }
}

public static void sortDemo(){
  List<String> list = new ArrayList<String>();
  list.add("aeg");
  list.add("e");
  list.add("rehhj");
  list.add("rtyr");
  list.add("nfgjdf");
  list.add("e");
  sop(list);
  Collections.sort(list);//自然排序
  sop(list);
  Collections.sort(list,new StrLenComparator());//自定义一个比较器排序
  sop(list);
 }
 public static void sop(Object o){
  System.out.println(o);
 }

(2)static <T extends Object & Comparable<? super T>> T  max(Collection<? extends T> coll):根据元素的自然顺序,返回给定 collection 的最大元素。

static <T> T  max(Collection<? extends T> coll, Comparator<? super T> comp):根据指定比较器产生的顺序,返回给定 collection 的最大元素。

static <T extends Object & Comparable<? super T>> T  min(Collection<? extends T> coll):根据元素的自然顺序 返回给定 collection 的最小元素。

static <T> T  min(Collection<? extends T> coll, Comparator<? super T> comp):根据指定比较器产生的顺序,返回给定 collection 的最小元素。

运行示例:

sop("最大值:"+Collections.max(list));//取出最大值

sop("最大值:"+Collections.max(list,new StrLenComparator()));//通过自定义比较器排序后取出最大值

注意:min的用法与max雷同。

(3)static <T> int  binarySearch(List<? extends Comparable<? super T>> list, T key):使用二分搜索法搜索指定列表,以获得指定对象。

static <T> int  binarySearch(List<? extends T> list, T key, Comparator<? super T> c):使用二分搜索法搜索指定列表,以获得指定对象。

注意:在使用二分搜索法之前,必须先对集合进行排序。

运行示例:

Collections.sort(list);

sop("自然排序集合:"+list);

int index = Collections.binarySearch(list,"rehhj");

sop("字符串的下标是:"+index);

*******************************************************

Collections.sort(list,new StrLenComparator());

sop("比较器排序集合:"+list);

int index = Collections.binarySearch(list, "aaaa", new StrLenComparator());

sop("字符串的下标是:"+index);

(4)static <T> void  copy(List<? super T> dest, List<? extends T> src):将所有元素从一个列表复制到另一个列表。

运行示例:

List<String> newlist = new ArrayList<String>();

Collections.copy(list,newlist);

sop(newlist);

--------------------------------------------------------------------------------

static <T> void  fill(List<? super T> list, T obj):使用指定元素替换指定列表中的所有元素。

运行示例:

Collections.fill(list, "456");//全部替换

sop(list);

--------------------------------------------------------------------------------

static void reverse(List<?> list):反转指定列表中元素的顺序。

运行示例:

Collections.reverse(list);

sop(list);

--------------------------------------------------------------------------------

逆转排序

static <T> Comparator<T> reverseOrder():返回一个比较器,它强行逆转实现了 Comparable 接口的对象 collection 的自然顺序。(自然顺序是通过对象自身的 compareTo 方法强行排序的。)

public static <T> Comparator<T> reverseOrder(Comparator<T> cmp):返回一个比较器,它强行逆转指定比较器的顺序。如果指定比较器为 null,则此方法等同于 reverseOrder()。(换句话说,它返回一个比较器,该比较器将强行逆转实现了 Comparable 接口的对象 collection 的自然顺序)。

运行示例:

TreeSet<String> ts = new TreeSet<String>(Collections.reverseOrder());

TreeSet<String> ts = new TreeSet<String>(Collections.reverseOrder(new StrLenComparator()));
  ts.add("aeg");
  ts.add("e");
  ts.add("rehhj");
  ts.add("rtyr");
  ts.add("nfgjdf");
  ts.add("e");

  sop(ts);

static void swap(List<?> list, int i, int j):在指定列表的指定位置处交换元素。

static void shuffle(List<?> list):使用默认随机源对指定列表进行置换。

运行示例:

Collections.swap(list,1,3);//交换指定位置的元素

Collections.shuffle(list);//随机排序

--------------------------------------------------------------------------------

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值