In Java, I have the following method:
public String normalizeList(List<String> keys) {
// ...
}
I want to check that keys:
- Is not
nullitself; and - Is not empty (
size() == 0); and - Does not have any
Stringelements that arenull; and - Does not have any
Stringelements that are empty (“”)
This is a utility method that will go in a “commons”-style JAR (the class wil be something like DataUtils). Here is what I have, but I believe its incorrect:
public String normalize(List<String> keys) {
if(keys == null || keys.size() == 0 || keys.contains(null) || keys.contains(""))
throw new IllegalArgumentException("Bad!");
// Rest of method...
}
I believe the last 2 checks for keys.contains(null) and keys.contains("") are incorrect and will likely thrown runtime exceptions. I know I can just loop through the list inside the if statement, and check for nulls/empties there, but I’m looking for a more elegant solution if it exists.
Doesn’t throw any runtime exceptions and results
trueif your list has either null (or) empty String.