简体   繁体   English

一个语句中的大于和小于

[英]Greater than and less than in one statement

I was wondering, do you have a neat way of doing this ?我想知道,你有一个巧妙的方法来做到这一点吗?

if(orderBean.getFiles().size() > 0  && orderBean.getFiles().size() < 5)

without declaring a variable that is not needed anywhere else ?不声明其他地方不需要的变量?

int filesCount = orderBean.getFiles().size();
if(filesCount > 0  && filesCount < 5) {

I mean, in for loop we are "declaring conditions" for the actual iteration, one can declare a variable and then specify the conditions.我的意思是,在 for 循环中,我们为实际迭代“声明条件”,可以声明一个变量,然后指定条件。 Here one can't do it, and neither can do something like在这里,一个人做不到,也不能做类似的事情

if(5 > orderBean.getFiles().size() > 0)

Simple utility method:简单实用方法:

public static boolean isBetween(int value, int min, int max)
{
  return((value > min) && (value < max));
}

Several third-party libraries have classes encapsulating the concept of a range , such as Apache commons-lang's Range (and subclasses).一些第三方库具有封装范围概念的类,例如 Apache commons-lang 的Range (和子类)。

Using classes such as this you could express your constraint similar to:使用这样的类,您可以表达类似于以下内容的约束:

if (new IntRange(0, 5).contains(orderBean.getFiles().size())
// (though actually Apache's Range is INclusive, so it'd be new Range(1, 4) - meh

with the added bonus that the range object could be defined as a constant value elsewhere in the class.额外的好处是范围对象可以定义为类中其他地方的常量值。

However, without pulling in other libraries and using their classes, Java's strong syntax means you can't massage the language itself to provide this feature nicely.但是,如果不引入其他库并使用它们的类,Java 的强大语法意味着您无法通过调整语言本身来很好地提供此功能。 And (in my own opinion), pulling in a third party library just for this small amount of syntactic sugar isn't worth it.而且(在我自己看来),仅仅为了这少量的语法糖而引入第三方库是不值得的。

If getFiles() returns a java.util.Collection , !getFiles().isEmpty() && size<5 can be OK.如果 getFiles() 返回java.util.Collection!getFiles().isEmpty() && size<5就可以了。

On the other hand, unless you encapsulate the container which provides method such as boolean sizeBetween(int min, int max) .另一方面,除非您封装提供诸如boolean sizeBetween(int min, int max)等方法的容器。

This is one ugly way to do this.这是一种丑陋的方法。 I would just use a local variable.我只会使用局部变量。

EDIT: If size() > 0 as well.编辑:如果 size() > 0 也是如此。

if (orderBean.getFiles().size() + Integer.MIN_VALUE-1 < Integer.MIN_VALUE + 5-1)

java is not python. java不是python。

you can't do anything like this你不能做这样的事情

if(0 < i < 5) or if(i in range(0,6))

you mentioned the easiest way :你提到了最简单的方法:

int i = getFilesSize();
if(0 < i && i < 5){
    //operations

}

of

   if(0 < i){
       if(i < 5){
          //operations
       }
     }

If this is really bothering you, why not write your own method isBetween(orderBean.getFiles().size(),0,5) ?如果这真的困扰您,为什么不编写自己的方法isBetween(orderBean.getFiles().size(),0,5)

Another option is to use isEmpty as it is a tad clearer:另一种选择是使用isEmpty因为它更清晰一点:

if(!orderBean.getFiles().isEmpty() && orderBean.getFiles().size() < 5)

Please just write a static method somewhere and write:请在某处写一个静态方法并写:

if( isSizeBetween(orderBean.getFiles(), 0, 5) ){
    // do your stuff 
}

Nowadays you can use lodash :现在你可以使用lodash

var x = 1;
var y = 5;
var z = 3;
_.inRange(z,x,y);
//results true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM