集合-Collections及常用方法

一. 概述

Collections类是Java提供的一个操作Set、List、Map等集合的工具类Collections 类提供了许多操作集合的静态方法,借助这些静态方法可以实现对集合元素的排序、查找替换和线程安全化等操作
Collections类中的方法都是静态的
Collections类中没有构造函数,不能进行实例化

二. 常用方法

1. 排序

 

复制代码
/*1. 根据元素的自然顺序对指定List集合的元素按升序进行排序:
List集合中所有元素必须实现Comparable接口;
此方法只适用于List集合
*/
static <T extends Comparable<? super T>> void   sort(List<T> list);
/*2. 根据指定比较器的顺序对List集合元素进行排序:
此方法只适用于List集合
*/
/*
按照元素自然顺序排序
Collections.sort(list); 
按照字符串长度比较器进行排序
Collections.sort(list,new StrLenComparator());
*/
//字符串长度比较器
class StrLenComparator implements Comparator<String>{
  public int compare(String o1, String o2) {
    if(o1.length()>o2.length())
    return 1;
    if(o1.length()<o2.length())
    return -1;
    return o1.compareTo(o2);
  }
}
static <T> void   sort(List<T> list,Comparator<? super T> c);
复制代码

 

 

 

2. 查找最值

 

复制代码
/*1. 根据元素的自然顺序,返回集合中的最大元素:
集合中所有元素必须实现Comparable接口;
此方法只适用于Collection集合
*/
static <T exntends Object & Comparable<? super T>> T   max(Collection<? extends T> coll);
/*2. 根据指定比较器的顺序,返回集合中的最大元素:
此方法只适用于Collection集合
*/
static <T> T   max(Collection<? entends T> coll,Comparator<? super T> comp);
/*3. 根据元素的自然顺序,返回集合中的最小元素:
集合中所有元素必须实现Comparable接口;
此方法只适用于Collection集合
*/
static <T exntends Object & Comparable<? super T>> T   min(Collection<? extends T> coll);
/*4. 根据指定比较器的顺序,返回集合中的最小元素:
此方法只适用于Collection集合
*/
static <T> T   min(Collection<? entends T> coll,Comparator<? super T> comp);

//最大元素
String maxStr = Collections.max(list);
//最小元素
String minStr = Collections.min(list);
复制代码

 

3. 二分搜索法

复制代码
/*1. 使用二分搜索法搜索指定的List集合,以获得指定对象在List集合中的索引:
要使该方法可以正常工作,List集合中的元素要先按自然顺序升序排列;
此方法只适用于List集合
*/
static <T> int   binarySearch(List<? entends Comparable<? super T>> list,T key);
/*2. 使用二分搜索法搜索指定的List集合,以获得指定对象在List集合中的索引:
要使该方法可以正常工作,List集合中的元素要先按指定比较器进行升序排列;
此方法只适用于List集合
*/
 static <T> int   binarySearch(List<? extends T> list,T key,Comparator<? super T> c);


//注意一定要保证原集合有序呦 (*^▽^*)
Collections.sort(list);
//二分搜索集合list中str的位置下标,若不存在则返回一个小于0的数
int index1=Collections.binarySearch(list,str);
//集合list先按照自定义比较器进行排序后,再对str进行二分搜索
int index2=Collections.binarySearch(list,str,new StrLenComparator());
复制代码

 

4. 替换

复制代码
/*1. 使用指定元素obj替换指定List集合中的所有元素:
此方法只适用于List集合
*/
static <T> void   fill(List<? super T> list,T obj);
/*2. 使用一个新值newVal代替List集合中的所有旧值oldVal:
此方法只适用于List集合;
此方法是按着元素替换,List集合中的set(int index,E element)是按着脚标替换
*/
static <T> boolean   replaceAll(List<T> list,T odlVal,T newVal);

//将list中的所有元素全部替换成为str
Collections.fill(list,str);

//将list中的所有oldStr全部替换为newStr
Collections.replaceAll(list,oldStr,newStr);

当然,要是想将list集合中的某个区间内的元素替换也是有办法滴(* ̄︶ ̄)
//定义一个方法,用来实现该要求,传入的参数为list集合,下标区间以及需要替换成的newVal
public static void fillSome(List<String> list,int startIndex,int endIndex,String newVal){
        //定义一个新集合
        List<String> li=new ArrayList<String>();
        for(int i = stratIndex;i<=endIndex;i++){
            //将需要替换的元素添加到新集合中
            li.add(list.get(i));
            //移除原集合中需要替换的元素
            list.remove(i);
        }
        //新集合中的元素替换成指定元素newVal
        Collections.fill(li,newVal);
        //将替换后的新元素添加到原集合中
        list.addAll(startIndex,li);
    }
复制代码

 

5. 反转

复制代码
/*1. 反转指定列表中元素的顺序:
此方法只适用于List集合
*/
static void   reverse(List<?> list);
/*2. 返回1个比较器,它对实现Comparable接口的对象集合施加了按自然排序反序的排列
*/
static <T> Comparator<T>   reverseOrder();
/*3. 返回1个比较器,它对按指定比较器排序的对象集合施加了按该比较器排序反序的排列
*/
static <T> Comparator<T>   reverseOrder(Comparator<T> cmp);


//反转当前集合
Collections.reverse(list);
复制代码

 6. 交换

/*交换列表中指定位置的元素:
此方法只适用于List集合
*/
static void   swap(List<?> list,int i,int j);

//交换集合list中下标为1和2的两个元素
Collections.swap(list,1,2);

 

 

  

 

posted @   苏汐sama  阅读(219)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
点击右上角即可分享
微信分享提示