Java中集合类的操作类Collections
  TEZNKK3IfmPf 2023年11月14日 43 0

Collections 是集合类的一个工具类,其提供了一系列静态方法,用于对集合中元素进行排序、搜索以及线程安全等各种操作
1) 排序(Sort)
    使用sort方法可以根据元素的自然顺序,对指定列表进行排序。列表中的所有元素都必须实现 Comparable 接口。或此列表内的所有元素都必须是使用指定比较器可相互比较的   Collections.sort(list , new Comparator(){ } );
2) 混排(Shuffling)
    混排算法所做的正好与 sort 相反: 它打乱在一个 List 中可能有的任何排列的踪迹。也就是说,基于随机源的输入重排该 List, 这样的排列具有相同的可能性(假设随机源是公正的)。这个算法在实现一个碰运气的游戏中是非常有用的。例如,它可被用来混排代表一副牌的 Card 对象的一个 List 。另外,在生成测试案例时,它也是十分有用的。Collections.Shuffling(list)
3) 反转(Reverse)
    使用Reverse方法可以将元素进行反转。Collections.reverse(list)
4) 替换所有的元素(Fill)
     使用指定元素替换指定列表中的所有元素。Collections.fill(li,"aaa");
5) 拷贝(Copy)
    用两个参数,一个目标 List 和一个源 List, 将源的元素拷贝到目标,并覆盖它的内容。目标 List 至少与源一样长。如果它更长,则在目标 List 中的剩余元素不受影响。Collections.copy(list,li): 后面一个参数是目标列表 ,前一个是源列表
6) 返回Collections中最小元素(min)
    根据指定比较器产生的顺序,返回给定 collection 的最小元素。collection 中的所有元素都必须是通过指定比较器可相互比较的
Collections.min(list)
7) 返回Collections中最大元素(max)
     根据指定比较器产生的顺序,返回给定 collection 的最大元素。collection 中的所有元素都必须是通过指定比较器可相互比较的
  Collections.max(list)
8) lastIndexOfSubList
    返回指定源列表中最后一次出现指定目标列表的起始位置,int count = Collections.lastIndexOfSubList(list,li);
9) IndexOfSubList
    返回指定源列表中第一次出现指定目标列表的起始位置,int count = Collections.indexOfSubList(list,li);
10) Rotate
根据指定的距离循环移动指定列表中的元素,Collections.rotate(list,-1);如果是负数,则正向移动,正数则方向移动

Student类

public class Student implements Comparable<Student> {
    int id;
    public Student(){           //无参构造函数
    }
    public Student(int id){     //有参构造函数
        this.id=id;
    }

    public int compareTo(Student o){
        return this.id-o.id;
    }
}

主类

import java.util.ArrayList;
import java.util.Collections;

public class Main {
    public static void main(String[] args) {
        ArrayList<Student> ss = new ArrayList<Student>();
        ss.add(new Student(5));      //增加一个元素
        ss.add(new Student(3));      //增加一个元素
        ss.add(new Student(7));      //增加一个元素
        ss.add(new Student(1));      //增加一个元素
        for(Student s:ss){
            System.out.println(s.id);
        }

        System.out.println("-----------排序-----------");
        Collections.sort(ss);
        for(Student s:ss){
            System.out.println(s.id);
        }

        System.out.println("-----------反转-----------");
        Collections.reverse(ss);
        for(Student s:ss){
            System.out.println(s.id);
        }

        System.out.println("-----------混排-----------");
        Collections.shuffle(ss);
        for(Student s:ss){
            System.out.println(s.id);
        }

        System.out.println("-----------返回最小元素-----------");
        Student min = Collections.min(ss);
        System.out.println(min.id);

        System.out.println("-----------返回最大元素-----------");
        Student max = Collections.max(ss);
        System.out.println(max.id);


    }
}

运行截图

Java中集合类的操作类Collections

相关文章:Java中的集合类

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月14日 0

暂无评论

推荐阅读
  TEZNKK3IfmPf   24天前   51   0   0 java
  TEZNKK3IfmPf   2024年05月31日   55   0   0 java
TEZNKK3IfmPf