There is no easy way to call a method for each item in a collection in Java; you need to manually create a new array of the correct size, go through the elements in the original array and sequentially add their lower counterpart to the new array.
However, given what you are specifically trying to do, you do not need to compare the entire array at once to do this, and bear the cost of copying everything. You can simply iterate over the array - if you find an element that is not equal to its string version, you can return false
. If you reach the end of the array without finding such an element, you can return true
.
This would be actually more effective because:
- You can further evaluate the short circuit if you find an element that has uppercase characters. (Imagine the case where the first element of an array in a millionth row is uppercase, you just saved a million calls in lowercase ()).
- You do not need to allocate memory for the entire additional array, which you will not use outside of the comparison.
- Even in the best case, your proposed scenario will include one iteration through an array to get lowercase versions, and then another iteration through an array to implement equal. Executing as in one iteration is likely to be more efficient even without the possibility of a short circuit.
source share