当前位置:首页 > 单片机 > 架构师社区
[导读]作者:蓝笔头链接:https://www.jianshu.com/p/f3e64e70eb1b1.排序1.1数组排序(`java.util.Arrays`)1.1.1基本数据类型排序对整个数组排序public static void sort(int[] a);对部分数组[fro...

作者:蓝笔头
链接:https://www.jianshu.com/p/f3e64e70eb1b

1. 排序

1.1 数组排序(`java.util.Arrays`)

1.1.1 基本数据类型排序
  • 对整个数组排序

public static void sort(int[] a);
  • 对部分数组 [fromIndex, toIndex) 排序

public static void sort(int[] a, int fromIndex, int toIndex);
七种基本类型 intlongshortcharbytefloatdouble(除了 boolean),都支持上述格式的排序 API

1.1.2 对象排序
  • 实现了 java.lang.Comparable 接口的对象。

// 对整个数组排序
public static void sort(Object[] a);
// 对部分数组 [fromIndex, toIndex) 排序
public static void sort(Object[] a, int fromIndex, int toIndex);
  • 通过 java.util.Comparator 排序:

void sort(T[] a, Comparator c); // 对部分数组 [fromIndex, toIndex) 排序 public staticvoid sort(T[] a, int fromIndex, int toIndex, Comparator c); public interface Comparator{ // result < 0:o1 排在 o2 前面 // result == 0:o1 和 o2 的值一样 // result > 0:o1 排在 o2 后面 int compare(T o1, T o2); } " data-snippet-id="ext.3b4fd31d611c03007a2fb4e82c943c31" data-snippet-saved="false" data-codota-status="done" style="font-size: inherit; color: inherit; line-height: inherit;">// 对整个数组排序
public static  void sort(T[] a, Comparatorsuper T> c);
// 对部分数组 [fromIndex, toIndex) 排序
public static  void sort(T[] a, int fromIndex, int toIndex,
                                Comparatorsuper
 T> c)
;

public interface Comparator<T{
    // result < 0:o1 排在 o2 前面
    // result == 0:o1 和 o2 的值一样
    // result > 0:o1 排在 o2 后面
    int compare(T o1, T o2);
}
案例:

() { @Override public int compare(Person o1, Person o2) { return o1.id - o2.id; } }); // 输出: // Solution.Person(id=1) // Solution.Person(id=2) Arrays.stream(persons).forEach(System.out::println); } @AllArgsConstructor @ToString public static class Person { private int id; } } " data-snippet-id="ext.9f53044395f3063f1ff57b840ee3a007" data-snippet-saved="false" data-codota-status="done" style="font-size: inherit; color: inherit; line-height: inherit;">public class Solution {
    public static void main(String[] args) {
        Person[] persons = new Person[2];
        persons[0] = new Person(2);
        persons[1] = new Person(1);

        Arrays.sort(persons, new Comparator() {
            @Override
            public int compare(Person o1, Person o2) {
                return o1.id - o2.id;
            }
        });

        // 输出:
        // Solution.Person(id=1)
        // Solution.Person(id=2)
        Arrays.stream(persons).forEach(System.out::println);
    }

    @AllArgsConstructor
    @ToString
    public static class Person {
        private int id;
    }
}
或者使用 lambda 表达式替换 Comparator 匿名类。

{ return o1.id - o2.id; }); " data-snippet-id="ext.4973bc6f1d0213eb013aab093375a4f4" data-snippet-saved="false" data-codota-status="done" style="font-size: inherit; color: inherit; line-height: inherit;">        Arrays.sort(persons, (o1, o2) -> {
            return o1.id - o2.id;
        });

1.2 列表排序(`java.util.Collections`)

  • 排序

> void sort(Listlist); public staticvoid sort(Listlist, Comparator c); public interface Comparator{ // result < 0:o1 排在 o2 前面 // result == 0:o1 和 o2 的值一样 // result > 0:o1 排在 o2 后面 int compare(T o1, T o2); } " data-snippet-id="ext.48d424bc07d1782333e834b6ae0da4ae" data-snippet-saved="false" data-codota-status="done" style="font-size: inherit; color: inherit; line-height: inherit;">public static super T>> void sort(List list);

public static  void sort(List list, Comparatorsuper T> c);

public interface Comparator<T{
    // result < 0:o1 排在 o2 前面
    // result == 0:o1 和 o2 的值一样
    // result > 0:o1 排在 o2 后面
    int compare(T o1, T o2);
}
  • 反转列表元素

public static void reverse(List list);

1.3 二维数组排序(`java.util.Arrays`)

提示:Java 数组也是一种对象

void sort(T[] a, Comparator c); // 案例 Arrays.sort(nums, (int[]a, int[]b) -> a[0] - b[0]); " data-snippet-id="ext.d85d29980807199ad798d55caa97768a" data-snippet-saved="false" data-codota-status="done" style="font-size: inherit; color: inherit; line-height: inherit;">// api
public static  void sort(T[] a, Comparatorsuper T> c);

// 案例
Arrays.sort(nums, (int[]a, int[]b) -> a[0] - b[0]);

2. 二分查找

  • 数组(java.util.Arrays

int binarySearch(T[] a, T key, Comparator c); public staticint binarySearch(T[] a, int fromIndex, int toIndex, T key, Comparator c); " data-snippet-id="ext.f23c7e0970895182c737a49fa0ca175a" data-snippet-saved="false" data-codota-status="done" style="font-size: inherit; color: inherit; line-height: inherit;">public static int binarySearch(int[] a, int key);
public static int binarySearch(int[] a, int fromIndex, int toIndex, int key);

public static int binarySearch(Object[] a, Object key);
public static int binarySearch(Object[] a, int fromIndex, int toIndex, Object key);

public static  int binarySearch(T[] a, T key, Comparatorsuper T> c);
public static  int binarySearch(T[] a, int fromIndex, int toIndex, T key, Comparatorsuper T> c);
  • 列表(java.util.Collections

int binarySearch(List> list, T key); public staticint binarySearch(List list, T key, Comparator c); " data-snippet-id="ext.f8d1fe37f82f9bfa0e5240865d0bfa44" data-snippet-saved="false" data-codota-status="done" style="font-size: inherit; color: inherit; line-height: inherit;">public static  int binarySearch(List extends Comparable super T>> list, T key);

public static  int binarySearch(List extends T> list, T key, Comparator super T> c);

3. 栈(`java.util.Stack`)

  • 创建

Stack stack = new Stack<>();
  • 数据操作

// 往【栈】里面添加一个元素
public E push(E item)

// 往【栈】里面弹出一个元素
public synchronized E pop()
;
  • 条件判断

public synchronized boolean isEmpty();

4. 队列(`java.util.Queue`)

  • 创建(java.util.LinkedList

Queue queue = new LinkedList<>();
  • 数据操作

// 往【队列】里面添加一个元素
boolean add(E e);

// 往【队列】里面弹出一个元素
poll();
  • 条件判断

boolean isEmpty();

5. 堆(`java.util.PriorityQueue`)

提示:Java 里面的优先队列

  • 创建

minHeap = new PriorityQueue<>(); // 创建一个最大堆 PriorityQueuemaxHeap = new PriorityQueue<>(Comparator.reverseOrder()); " data-snippet-id="ext.b81abce1e0996d64116eee23b0b4a398" data-snippet-saved="false" data-codota-status="done" style="font-size: inherit; color: inherit; line-height: inherit;">// 创建一个最小堆
PriorityQueue minHeap = new PriorityQueue<>();

// 创建一个最大堆
PriorityQueue maxHeap = new PriorityQueue<>(Comparator.reverseOrder());
  • 数据操作

// 往【堆】里面添加一个元素
public boolean add(E e);

// 从【堆】里面弹出一个元素
public E poll();

其他工具

  • 降序排序(java.util.Comparator

reversed(); // 反转一个 Comparable 的排序规则 // 比如从【升序】反转为【降序】 public static> ComparatorreverseOrder(); " data-snippet-id="ext.5516f5b09794f57947cbef89383120cd" data-snippet-saved="false" data-codota-status="done" style="font-size: inherit; color: inherit; line-height: inherit;">// 反转一个 Comparator 的排序规则
// 比如从【升序】反转为【降序】
default Comparator reversed();

// 反转一个 Comparable 的排序规则
// 比如从【升序】反转为【降序】
public static super T>> Comparator reverseOrder();
  • 大数(java.math.BigInteger

// 创建一个大数
public static BigInteger valueOf(long val);

// 数据操作
public BigInteger add(BigInteger val);
public BigInteger subtract(BigInteger val);
public BigInteger multiply(BigInteger val);
public BigInteger divide(BigInteger val);
  • 集合(java.util.Collections

ListnCopies(int n, T o); // 反转一个 list 的顺序 public static void reverse(List list); " data-snippet-id="ext.f8838765f10b2f138e21a8d0a0215ea6" data-snippet-saved="false" data-codota-status="done" style="font-size: inherit; color: inherit; line-height: inherit;">// 初始化一个具有 n 个相同元素 o 的 list
public static  List nCopies(int n, T o);

// 反转一个 list 的顺序
public static void reverse(List list);

本站声明: 本文章由作者或相关机构授权发布,目的在于传递更多信息,并不代表本站赞同其观点,本站亦不保证或承诺内容真实性等。需要转载请联系该专栏作者,如若文章内容侵犯您的权益,请及时联系本站删除。
换一批
延伸阅读

在微服务架构逐渐成为主流的今天,系统被拆分为多个独立的微服务,每个微服务都有自己的API接口。随着微服务数量的不断增加,系统面临的流量管控、安全防护、服务治理等问题也日益突出。API网关作为微服务架构的入口层,能够统一管...

关键字: API 网关

东京2026年2月1日 /美通社/ -- NTT DOCOMO, INC.与Aduna今日共同宣布,双方已于2026年1月29日签署合作协议(简称“协议”)。 此次合作将使D...

关键字: API COM 网络 AD

新增手表、手链、戒指、耳环、项链、围巾、帽子、鞋子、包袋九大品类虚拟试穿API,完善 AI 驱动时尚生态,赋能全球品牌与开发者打造全场景穿搭体验 上海2026年1月22日 /美通社/ -- 全球领先的人工智能与增强现实...

关键字: API 移动 AI BSP

正值成立 25 周年之际,维基百科母公司维基媒体基金会(Wikimedia)于当地时间 1 月 15 日通过博客宣布,已与亚马逊、Meta、微软、Mistral AI 及 Perplexity 等多家科技企业达成新合作。

关键字: 维基百科 AI API 网络爬虫

在数字化转型加速的2025年,API已成为企业数据交互的核心通道。随着物联网、云计算和微服务架构的普及,API接口的安全防护面临前所未有的挑战。据行业报告显示,超过92%的数据泄露事件源于API攻击,涉及金融、电商、医疗...

关键字: API数据安全 API

阿布扎比2025年12月7日 /美通社/ -- 12月7日,壳牌携全新升级的壳牌超凡喜力极速闪电系列润滑油(以下简称"闪电红2.0")亮相阿布扎比。作为法拉利车队的甄选用油,"闪电红&quo...

关键字: 发动机 奔驰 宝马 API

-Nomad eSIM宣布业务实现强劲增长,企业级与消费级产品线全面扩展 加利福尼亚州圣克拉拉2025年11月19日 /美通社/ -- Nomad eSIM是Lo...

关键字: ESIM AD API ENTERPRISE

技术供应商 API 选择 Datavault AI,以持续打造其领先的技术创新文化费城, Oct. 30, 2025 (GLOBE NEWSWIRE) -- 据 IBN 报道,专利数据代币化与货币化技术领军企业 Da...

关键字: API MEDIA AI ASDA

北京——2025年10月15日 亚马逊云科技近日宣布,Amazon Verified Permissions 已在由西云数据运营的亚马逊云科技中国(宁夏)区域和由光环新网运营的亚马逊云科技中国(北京)区域正式上线。该服务...

关键字: API 亚马逊云科技

新加坡2025年9月17日 /美通社/ -- 9月11日,第三届亚洲愿景论坛在新加坡拉开帷幕。本届论坛由财新国际主办,新加坡旅游局(STB)担任合作主办方。本届论坛延续其在促进国际对话方面取得的成功经验,以"变...

关键字: 新加坡 人工智能 AI技术 API
关闭