[英]How can I represent a range in Java?
假設一個整數應該在以下范圍內: [0...2147483647]
我想檢查一個整數變量是否在這個范圍內。 我知道它可以通過一個簡單的 if-else 語句來完成,但是有沒有更有效的方法來檢查它是否在范圍內?
我寧願不這樣做:
if (foo >= 0 && foo <= 2147483647)
{
// do something
}
Apache Commons Lang 有一個Range
類用於執行任意范圍。
Range<Integer> test = Range.between(1, 3);
System.out.println(test.contains(2));
System.out.println(test.contains(4));
Guava Range
有類似的 API。
如果您只想檢查數字是否適合 long 值或 int 值,您可以嘗試通過BigDecimal
使用它。 如果值對於這些精度來說太大,則有longValueExact
和intValueExact
方法會拋出異常。
您可以創建一個類來表示這一點
public class Range
{
private int low;
private int high;
public Range(int low, int high){
this.low = low;
this.high = high;
}
public boolean contains(int number){
return (number >= low && number <= high);
}
}
示例用法:
Range range = new Range(0, 2147483647);
if (range.contains(foo)) {
//do something
}
我知道這是一個很老的問題,但是使用 Java 8 的 Streams 您可以獲得一系列int
如下所示:
// gives an IntStream of integers from 0 through Integer.MAX_VALUE
IntStream.rangeClosed(0, Integer.MAX_VALUE);
然后你可以做這樣的事情:
if (IntStream.rangeClosed(0, Integer.MAX_VALUE).matchAny(n -> n == A)) {
// do something
} else {
// do something else
}
您可以使用java.time.temporal.ValueRange
它接受long
並且也可以與int
:
int a = 2147;
//Use java 8 java.time.temporal.ValueRange. The range defined
//is inclusive of both min and max
ValueRange range = ValueRange.of(0, 2147483647);
if(range.isValidValue(a)) {
System.out.println("in range");
}else {
System.out.println("not in range");
}
如果您要檢查很多區間,我建議使用區間樹。
無論您嘗試優化這種不那么密集的計算的效率如何,您都將進行 if 檢查 :) 您可以從數字中減去上限,如果它是正數,您就知道超出了范圍。 您也許可以執行一些布爾位移邏輯來解決這個問題,如果您願意,您甚至可以使用費馬定理(開玩笑:)但關鍵是“為什么”需要優化這種比較? 目的是什么?
對於一系列Comparable
我使用以下內容:
public class Range<T extends Comparable<T>> {
/**
* Include start, end in {@link Range}
*/
public enum Inclusive {START,END,BOTH,NONE }
/**
* {@link Range} start and end values
*/
private T start, end;
private Inclusive inclusive;
/**
* Create a range with {@link Inclusive#START}
* @param start
*<br/> Not null safe
* @param end
*<br/> Not null safe
*/
public Range(T start, T end) { this(start, end, null); }
/**
* @param start
*<br/> Not null safe
* @param end
*<br/> Not null safe
*@param inclusive
*<br/>If null {@link Inclusive#START} used
*/
public Range(T start, T end, Inclusive inclusive) {
if((start == null) || (end == null)) {
throw new NullPointerException("Invalid null start / end value");
}
setInclusive(inclusive);
if( isBigger(start, end) ) {
this.start = end; this.end = start;
}else {
this.start = start; this.end = end;
}
}
/**
* Convenience method
*/
public boolean isBigger(T t1, T t2) { return t1.compareTo(t2) > 0; }
/**
* Convenience method
*/
public boolean isSmaller(T t1, T t2) { return t1.compareTo(t2) < 0; }
/**
* Check if this {@link Range} contains t
*@param t
*<br/>Not null safe
*@return
*false for any value of t, if this.start equals this.end
*/
public boolean contains(T t) { return contains(t, inclusive); }
/**
* Check if this {@link Range} contains t
*@param t
*<br/>Not null safe
*@param inclusive
*<br/>If null {@link Range#inclusive} used
*@return
*false for any value of t, if this.start equals this.end
*/
public boolean contains(T t, Inclusive inclusive) {
if(t == null) {
throw new NullPointerException("Invalid null value");
}
inclusive = (inclusive == null) ? this.inclusive : inclusive;
switch (inclusive) {
case NONE:
return ( isBigger(t, start) && isSmaller(t, end) );
case BOTH:
return ( ! isBigger(start, t) && ! isBigger(t, end) ) ;
case START: default:
return ( ! isBigger(start, t) && isBigger(end, t) ) ;
case END:
return ( isBigger(t, start) && ! isBigger(t, end) ) ;
}
}
/**
* Check if this {@link Range} contains other range
* @return
* false for any value of range, if this.start equals this.end
*/
public boolean contains(Range<T> range) {
return contains(range.start) && contains(range.end);
}
/**
* Check if this {@link Range} intersects with other range
* @return
* false for any value of range, if this.start equals this.end
*/
public boolean intersects(Range<T> range) {
return contains(range.start) || contains(range.end);
}
/**
* Get {@link #start}
*/
public T getStart() { return start; }
/**
* Set {@link #start}
* <br/>Not null safe
* <br/>If start > end they are switched
*/
public Range<T> setStart(T start) {
if(start.compareTo(end)>0) {
this.start = end;
this.end = start;
}else {
this.start = start;
}
return this;
}
/**
* Get {@link #end}
*/
public T getEnd() { return end; }
/**
* Set {@link #end}
* <br/>Not null safe
* <br/>If start > end they are switched
*/
public Range<T> setEnd(T end) {
if(start.compareTo(end)>0) {
this.end = start;
this.start = end;
}else {
this.end = end;
}
return this;
}
/**
* Get {@link #inclusive}
*/
public Inclusive getInclusive() { return inclusive; }
/**
* Set {@link #inclusive}
* @param inclusive
*<br/>If null {@link Inclusive#START} used
*/
public Range<T> setInclusive(Inclusive inclusive) {
this.inclusive = (inclusive == null) ? Inclusive.START : inclusive;
return this;
}
}
(這是一個有點短的版本。完整的代碼可以在這里找到)
org.apache.commons.lang.math.NumberRange;
這是擴展org.apache.commons.lang.math.Range @pimaster答案的類
import java.util.Arrays;
class Soft{
public static void main(String[] args){
int[] nums=range(9, 12);
System.out.println(Arrays.toString(nums));
}
static int[] range(int low, int high){
int[] a=new int[high-low];
for(int i=0,j=low;i<high-low;i++,j++){
a[i]=j;
}
return a;
}
}
我的代碼類似於 Python 的范圍 :)
如果您使用 Spring,您可以依賴 org.springframework.data.domain,它非常完整,包括綁定和未綁定范圍。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.