A cumbersome approach would be to keep regenerating random strings till you find that you have encountered a unique random string that is not already contained in your array. All you have to do is check whether the newly generated random string is already in the array or not
quantity = 100000
insertedNum = 0;
length = 9;
String[] numGen = new String[100];
idx = 0;
String random = "";
while (insertedNum < quantity) {
random=RandomStringUtils.randomNumeric(length);
while( isAlreadyInArray( numGen , random ) )
{
//regenerate a random string
random = RandomStringUtils.randomNumeric( length );
}
// Add it to your array, and increment it by one
numGen[idx] = random;
idx = (idx + 1) % numGen.length;
// In this case, the array got populated to completion
if (idx == 0) {
System.out.println( java.util.Arrays.toString( numGen ) );
//clear the batch set
insertedNum += DB Code. If unique constraint error then discard batch return 0 else execute batch return inserted count.
numGen = new String[100];
}
}
And here is the helper method isAlreadyInArray( String[] array , String someVal )
public boolean isAlreadyInArray( String[] mData , String strToCheck )
{
for( int x = 0; x < mData.length; x++ )
{
if( mData[x] != null && mData[x].equalsIgnoreCase( strToCheck ) )
{
return true;
}
}
return false;
}
Please run this sample, and if it fails to help, then certainly we can re-iterate over the problem :)