比较Java枚举值 [英] Compare Java enum values

查看:120
本文介绍了比较Java枚举值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想检查一个枚举值是否与另一个值'大于/等于'?

解决方案

所有Java枚举都实现了Comparable: http://java.sun.com/javase/6/docs/api/java/lang/Enum.html



您还可以使用 ordinal 方法将其变为 int s,那么比较是微不足道的。

  if(ErrorLevel.ERROR.compareTo(someOtherLevel)< = 0){ 
...
}


Is there a way to check if an enum value is 'greater/equal' to another value?

I want to check if an error level is 'error or above'.

解决方案

All Java enums implements Comparable: http://java.sun.com/javase/6/docs/api/java/lang/Enum.html

You can also use the ordinal method to turn them into ints, then comparison is trivial.

if (ErrorLevel.ERROR.compareTo(someOtherLevel) <= 0) {
  ...
}

这篇关于比较Java枚举值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆