• Java排序学习


    int[] 数组排序

    升序排序

    Arrays.sort(num);
    
    • 1

    降序排序

    
    num= IntStream.of(num)          // 变为 IntStream
                    .boxed()           // 变为 Stream
                    .sorted(Comparator.reverseOrder()) // 按自然序相反排序
                    .mapToInt(Integer::intValue)       // 变为 IntStream
                    .toArray(); 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    Integer[]、String[] 数组排序

    升序排序:

    Arrays.sort(num);
    
    • 1

    降序排序:

         Arrays.sort(num, Collections.reverseOrder());
    
    • 1

    List排序

    升序排序:

    Collections.sort(num);
    
    • 1

    降序排序:

         Collections.sort(num, Collections.reverseOrder());
    
    • 1

    Set排序

    set不支持直接排序,可转换成List然后进行排序,需要频繁操作有序可以参考TreeSet👇
    TreeSet

    Set->List

    Set<Integer> numbers = new HashSet<>();
            numbers.add(5);
            numbers.add(2);
            numbers.add(8);
            numbers.add(1);
            numbers.add(9);
    
            // 将Set转换为List
            List<Integer> sortedList = new ArrayList<>(numbers);
    
            // 升序排序
            Collections.sort(sortedList);
            // 降序排序
            Collections.sort(sortedList, Collections.reverseOrder());
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    Map排序

    map也不支持直接排序
    可以使用TreeMap来进行**键(Key)**的排序。

    TreeMap<String, Integer> treeMap = new TreeMap<>(); //按key升序排序
    TreeMap<String, Integer> treeMap = new TreeMap<>(Collections.reverseOrder());//按key降序排序
    
    遍历
    	for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
    			System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
    		}
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    想实现当前数组通过别的数组进行排序(力扣2948题解题技巧)

    只能使用Integer不能使用int

    Integer[] ids = new Integer[n];
            for(int i=0;i<n;i++)
                ids[i] = i;
    Arrays.sort(ids,(i,j)->nums[i]-nums[j]);  //把id的数按照nums的大小进行排序。
    
    • 1
    • 2
    • 3
    • 4

    👆这个方法可以记录nums按值的大小排序时候的每个数的原始位置是多少(ids数组就是记录的位置)。

                //Arrays.copyOfRange左闭右开区间
                  Integer[] subIds = Arrays.copyOfRange(ids, begin, i);
    
    • 1
    • 2

    按对象中的某个值排序(对象数组)

    //int[][] rides
            Arrays.sort(rides, (a, b) -> a[1] - b[1]);  //按int[]数组中的第二个中排序(顺序)
    
    • 1
    • 2

    优先队列排序

     PriorityQueue<long[]> pq = new PriorityQueue<long[]>((a, b) -> Long.compare(a[0], b[0]));
    
    • 1

    👆这段代码是Java中创建一个PriorityQueue对象的示例,该对象用于存储long[](长整型数组)类型的元素,并根据数组中第一个元素的自然顺序(从小到大)进行排序。

    下面是代码的分解:

    PriorityQueue pq: 声明了一个名为pq的PriorityQueue对象,该对象用于存储long[](长整型数组)类型的元素。

    new PriorityQueue(…): 使用PriorityQueue的构造函数创建一个新的PriorityQueue对象。

    (a, b) -> Long.compare(a[0], b[0]): 这是一个Lambda表达式,它定义了一个比较器(Comparator),用于确定PriorityQueue中元素的顺序。

    a 和 b 是PriorityQueue中两个要比较的元素,它们都是long[]类型的数组。
    Long.compare(a[0], b[0]) 比较a数组的第一个元素和b数组的第一个元素。如果a[0]小于b[0],则返回负数;如果a[0]等于b[0],则返回零;如果a[0]大于b[0],则返回正数。

  • 相关阅读:
    javab每日一题:在spring里有哪些常用注解?
    [VIM]spcaevim
    云呐|固定资产管理系统功能包括哪些?
    html大学生网站开发实践作业:传统文化网页设计题材【绒花6页】HTML+CSS+JavaScript
    Redis启停脚本
    用Blender制作YOLO目标检测器训练数据
    YOLOV8改进:RefConv(即插即用!重参数化重聚焦卷积替代常规卷积,无额外推理成本下涨点明显!)
    ProPresenter 7 for Mac:Mac电脑好用的文稿演示软件
    Python 断言的使用
    win11安装IIS步骤-已验证23.10.10
  • 原文地址:https://blog.csdn.net/yusude123456/article/details/134099836