sql IN logical operation for java

by Hamid Reza Fahimi Madjd @ Apr 20, 2009

Hi,
have you ever needed sql IN for java language ?
OK, in this post I'm going to use new java 5 feature (varargs method) to simulate sql IN:

public static boolean in(int i, int... ints) {
Arrays.sort(ints);
return Arrays.binarySearch(ints, i) >= 0;
}

as you see, our function gets 2 parameters, first one is your desired value and second one is your search list.
now, when you call the method:

public static void main(String[] args) {
System.out.println(in(1, 2, 3, 4, 5, 1));
}

it returns:

true

i hope you find this tip useful;

java

add your comment

name (required)
email (required)
url
comment

not readable? sorry, click here

last tweet

Developed withDeveloped with
Clicky Web Analytics