首页 技术 正文
技术 2022年11月20日
0 收藏 793 点赞 3,222 浏览 2333 个字

在研究OpenJDK,Java编译器javac源码的过程中,发现以下代码。

顿时发现枚举类竟然也有如此“高端大气上档次”的用法。

沙场点兵(用法源码)

com.sun.tools.javac.file.JavacFileManager.SortFiles
protected enum SortFiles implements Comparator<File> {
FORWARD {
public int compare(File f1, File f2) {
return f1.getName().compareTo(f2.getName());
}
},
REVERSE {
public int compare(File f1, File f2) {
return -f1.getName().compareTo(f2.getName());
}
};
};

指点江山(代码说明)

1.枚举类SortFiles 实现了比较器Comparator接口。

2.真正实现了接口方法的是枚举类的元素FORWARD和REVERSE。

3.2个比较方法的实现区别仅在于“一个负号”“-”。

我以前看到的顺序逆序的比较代码,是以下形式的:

 return f1.getName().compareTo(f2.getName());
return f2.getName().compareTo(f1.getName());

这种形式的,仔细看才能看出差别。

没有“一个负号”直接“取反”来的简便。

别有洞天(受益匪浅)

以前在Java中使用枚举,与大学时学习C/C++时一样,最常用最熟悉的就是以下形式:

enum ItWebsite{ CSDN,ITEye,FansUnion};

自从多次看了JDK源码中枚举的用法,尤其是这次发现的“枚举实现接口”,真的“涨姿势”了。

现在越来越发现,研究开源代码,尤其是牛逼的JDK开源实现OpenJDK的源码,收获真是很大啊。

小试牛刀(使用示例)

public class Website {
//网站的名字
private String name; public Website(String name) {
this.name = name;
} public String getName() {
return name;
}
}
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;/**
* OpenJDK源码研究笔记(十):枚举的高级用法,枚举实现接口,竟是别有洞天
*
* @author LeiWen@FansUnion.cn
*
*/
public class EnumImplementsInterfaceExample { enum WebsiteSort implements Comparator<Website> {
// 网站的名字,大小比较
FORWAWD {
public int compare(Website w1, Website w2) {
return w1.getName().compareTo(w2.getName());
} },
// 网站的名字,大小比较,取反
REVERSE {
public int compare(Website w1, Website w2) {
return -w1.getName().compareTo(w2.getName());
} }
} public static void main(String[] args) {
List<Website> threeITWebsites = buildThreeITWebsites();
// 特别说明:java.util.Collections.sort 根据集合元素的自然顺序,按照升序排列。 // 顺序排序
Collections.sort(threeITWebsites, WebsiteSort.FORWAWD);
display(threeITWebsites); // 换行
System.out.println(); // 逆序排序
Collections.sort(threeITWebsites, WebsiteSort.REVERSE);
display(threeITWebsites);
} private static void display(List<Website> threeITWebsites) {
for (Website website : threeITWebsites) {
System.out.print(website.getName() + "\t");
} } // 构造3个IT技术网站
private static List<Website> buildThreeITWebsites() {
List<Website> websiteList = new ArrayList<Website>();
websiteList.add(new Website("CSDN.net"));
websiteList.add(new Website("ITEye.com"));
websiteList.add(new Website("FansUnion.cn"));
return websiteList;
}
}

有模有样(运行结果)

CSDN.net    FansUnion.cn    ITEye.com    
ITEye.com    FansUnion.cn    CSDN.net

相关阅读

我的CSDN博客专栏  OpenJDK源码研究笔记

OpenJDK源码研究过程中整理的学习笔记。 OpenJDK是GPL许可(GPL-licensed)的Java平台的开源实现。

原文参见http://FansUnion.cn/articles/3057(小雷网-FansUnion.cn)

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,071
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,549
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,397
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,174
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,809
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,889