• Java中collections类常用方法介绍 (#将指定集合包装成线程同步的集合)


    • 说明

    1.collections工具类是操作Collection、Map的工具类

    2.Collections 类中提供了多个 synchronizedXxx() 方法,该方法可使将指定集合包装成线程同步的集合,从而可以解决多线程并发访问集合时的线程安全问题

    3.思考:Collection 和 Collections的区别?

    • 常用方法

    将指定集合包装成线程同步的集合——synchronizedXxx(List)

    1. List list = new ArrayList();
    2. list.add(123);
    3. list.add(123);
    4. list.add(43);
    5. list.add(765);
    6. list.add(-97);
    7. list.add(0);
    8. //返回的list1即为线程安全的List
    9. List list1 = Collections.synchronizedList(list);

    反转 List 中元素的顺序——reverse(List)

     Collections.reverse(list);

    对 List 集合元素进行随机排序——shuffle(List)

     Collections.shuffle(list);

     将指定 list 集合中的 i 处元素和 j 处元素进行交换——swap(List,int i, int j)

    Collections.swap(list,1,3);

    根据元素的自然顺序对指定 List 集合元素按升序排序——sort(List)

    Collections.sort(list);

    根据指定的 Comparator 产生的顺序对 List 集合元素进行排序——sort(List,Comparator)

    1. List list = new ArrayList();
    2. list.add(123);
    3. list.add(43);
    4. list.add(765);
    5. list.add(-97);
    6. list.add(0);
    7. Collections.sort(list, new Comparator() {
    8. @Override
    9. public int compare(Object o1, Object o2){
    10. if (o1 instanceof Integer && o2 instanceof Integer){
    11. return -Integer.compare(((Integer) o1),(Integer) o2);
    12. }else {
    13. throw new RuntimeException("输入的格式不符合要求");
    14. }
    15. }
    16. });

    返回指定集合中指定元素的出现次数——int frequency(Collection,Object)

    int frequency = Collections.frequency(list, 123);

    将src中的内容复制到dest中——void copy(List dest,List src)

    1. //错误写法:
    2. // 报异常:IndexOutOfBoundsException("Source does not fit in dest")
    3. // List dest = new ArrayList();
    4. // Collections.copy(dest,list);
    5. //正确写法:
    6. List dest = Arrays.asList(new Object[list.size()]);
    7. System.out.println(dest.size());//list.size();
    8. Collections.copy(dest,list);
    9. System.out.println(dest);

    使用新值替换 List 对象的所有旧值——boolean replaceAll(List list,Object oldVal,Object newVal) 

    Collections.replaceAll(list,123,1234);

     max和min方法用的的集合

    1. //实例化的时候加了泛型,下边的定制排序写法不同
    2. List list = new ArrayList<>();
    3. list.add(123);
    4. list.add(43);
    5. list.add(765);
    6. list.add(-97);
    7. list.add(0);

    根据元素的自然顺序,返回给定集合中的最大元素——Object max(Collection)

    Integer max1 = Collections.max(list);//765

    根据 Comparator 指定的顺序,返回给定集合中的最大元素——Object max(Collection,Comparator)

    1. Integer max = Collections.max(list, new Comparator() {
    2. @Override
    3. public int compare(Integer o1, Integer o2) {
    4. return -Integer.compare(o1, o2);
    5. }
    6. });
    7. System.out.println(max);//-97

    根据元素的自然顺序,返回给定集合中的最小元素——Object min(Collection)

    Integer min1 = Collections.min(list);//-97

    根据 Comparator 指定的顺序,返回给定集合中的最小元素——Object min(Collection,Comparator)

    1. Integer min = Collections.min(list, new Comparator() {
    2. @Override
    3. public int compare(Integer o1, Integer o2) {
    4. return -Integer.compare(o1, o2);
    5. }
    6. });
    7. System.out.println(min);//765

  • 相关阅读:
    代码混淆界面介绍
    图论应用——拓扑排序
    【开发】React框架下如何集成H.265网页流媒体EasyPlayer.js视频播放器?
    基于关系抽取的相似度计算
    node的http模块
    Hadoop Spark太重,esProc SPL很轻
    tlaplus-vscode插件使用记录
    问题解决:使用VISIO导出为PDF,在Latex中有白色边框以及黑色线框的问题
    刷新AI作图速度,最快的开源Stable Diffusion出炉
    Flutter didUpdateWidget 的使用问题
  • 原文地址:https://blog.csdn.net/m0_58052874/article/details/126038902