Java in operator

无人久伴 提交于 2019-11-28 20:11:54
Aaron Digulla

Using op4j:

Op.onListFor(a,b,c).get().contains(value);

Using the same approach, you could create a helper classes Is with a method in:

class Is<T> {
    private T value;

    public Is( T value ) { this.value = value; }

    public boolean in( T... set ) {
        for( T item : set ) {
            if( value.equals( item ) ) {
                return true;
            }
        }

        return false;
    }

    public static <T> Is<T> is( T value ) {
        return new Is<T>( value );
    }
}

with a static import, you can write:

if(is(value).in(a,b,c)) {
}

You can write a helper method to do it.

public static <T> boolean isIn(T t, T... ts) {
    for(T t2: ts) 
      if (t.equals(t2)) return true;
    return false;
}

// later
if (isIn(value, a,b,c)) {

} else if (isIn(value, d,e)) {

}

There has been a very old proposal for collection literals.

Currently there is Sets.newHashSet in Guava which is pretty similar to Arrays.asList.

You are looking for the Java Community Process

I doubt something like an IN operator would be made available, as there are already multiple ways of doing this(like using switch) as you yourself pointed out.

And I think requirement list for project-coin and J8 is already fully loaded to be anything like this to be considered.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!