惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Simon Willison's Weblog
Simon Willison's Weblog
Help Net Security
Help Net Security
P
Privacy International News Feed
T
Threat Research - Cisco Blogs
C
Cisco Blogs
C
CERT Recently Published Vulnerability Notes
NISL@THU
NISL@THU
L
LINUX DO - 热门话题
Security Latest
Security Latest
A
Arctic Wolf
G
GRAHAM CLULEY
月光博客
月光博客
S
Securelist
D
Docker
J
Java Code Geeks
T
Troy Hunt's Blog
T
Tenable Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
SecWiki News
SecWiki News
S
Security @ Cisco Blogs
量子位
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
L
LINUX DO - 最新话题
Recent Commits to openclaw:main
Recent Commits to openclaw:main
aimingoo的专栏
aimingoo的专栏
博客园 - 【当耐特】
H
Heimdal Security Blog
The Hacker News
The Hacker News
博客园 - 三生石上(FineUI控件)
Application and Cybersecurity Blog
Application and Cybersecurity Blog
N
Netflix TechBlog - Medium
Vercel News
Vercel News
Forbes - Security
Forbes - Security
B
Blog RSS Feed
H
Hackread – Cybersecurity News, Data Breaches, AI and More
IT之家
IT之家
B
Blog
MongoDB | Blog
MongoDB | Blog
博客园 - 聂微东
Google DeepMind News
Google DeepMind News
S
Secure Thoughts
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
C
Check Point Blog
云风的 BLOG
云风的 BLOG
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
T
The Blog of Author Tim Ferriss
L
Lohrmann on Cybersecurity
F
Full Disclosure
D
Darknet – Hacking Tools, Hacker News & Cyber Security
P
Proofpoint News Feed

博客园 - 魔豆

oracle中使用unpivot实现列转行 推荐一个可以下载B站视频的工具 学习qt,做了一个小应用:随机点名提问系统 hadoop的eclipse插件 mysql5.5安装到最后一步卡死无响应的解决方法 Hbase各版本支持情况 css3实现动画效果 HTML5中使用EventSource实现服务器发送事件 HTML5中的Web Worker技术 css3中的盒子模型 使用visual studio code运行html 【转】解决chrome浏览器不支持audio和video标签的autoplay自动播放 spring mvc中添加对Thymeleaf的支持 Eclipse安装代码反编译插件Enhanced Class Decompiler 使用idea创建web项目 shell编程学习笔记(十二):Shell中的break/continue跳出循环 shell编程学习笔记(十一):Shell中的while/until循环 shell编程学习笔记(十):Shell中的for循环 shell编程学习笔记(九):Shell中的case条件判断
Comparable接口的使用
魔豆 · 2019-10-07 · via 博客园 - 魔豆

功能:

Comparable接口可用于对象的排序或者对象的分组

介绍:

Comparable接口强行对实现它的类的每个实例进行自然排序,该接口的唯一方法compareTo方法被称为自然比较方法

方法: int compareTo(Object   o)  

利用当前对象和传入的目标对象进行比较:

若是当前对象比目标对象大,则返回1,那么当前对象会排在目标对象的后面

若是当前对象比目标对象小,则返回-1,那么当前对象会排在目标对象的后面

若是两个对象相等,则返回0

实例:

import java.util.Arrays;

public class User implements Comparable<User> {
    
    public int age;
    public String username;

    public User(int age, String username) {
        this.age = age;
        this.username = username;
    }

    @Override
    public String toString() {
        return this.username;
    }

    @Override
    public int compareTo(User o) {
        if(this.age>o.age) {
            return 1;
        } else if(this.age<o.age) {
            return -1;
        } else {
            return 0;
        }
    }
    
    public static void main(String[] args) {
        User[] arr = new User[3];
        arr[0] = new User(15,"user1");
        arr[1] = new User(10,"user2");
        arr[2] = new User(20,"user3");
        
        System.out.println("排序前:");
        System.out.println(Arrays.toString(arr));
        
        Arrays.sort(arr);
        
        System.out.println("排序后:");
        System.out.println(Arrays.toString(arr));

    }

}

执行结果:

排序前:
[user1, user2, user3]
排序后:
[user2, user1, user3]