In Java, I have the following method:
public String normalizeList(List<String> keys) {
// ...
}
I want to check that keys
:
null
itself; andsize() == 0
); andString
elements that are null
; andString
elements 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.
keys.contains(null) || keys.contains("")
不抛出任何运行时异常和结果true
,如果你的列表中有任一空(或)空字符串。
This looks fine to me, the only exceptions you would get from keys.contains(null)
and keys.contains("")
would be if keys
itself was null
.
However since you check for that first you know that at this point keys
is not null
, so no runtime exceptions will occur.
You could also use Apache StringUtils and check if the string isBlank, this will check for null, Emptystring and will also trim your value.
if(StringUtils.isBlank(listItemString)){...}
Checkout StringUtils Docs here:
https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringUtils.html
I am not sure but isn't there any helper class in ApacheCommon library to do that? like when you have the isEmpty for a string and you have isNullOrEmpty in ApacheCommons library
Check the list in one pass
public static boolean empty(String s) {
if (s == null)
return true;
else if (s.length() == 0)
return true;
return false;
}
public String normalize(List<String> keys) {
if(keys == null || keys.size() == 0)
throw new IllegalArgumentException("Bad!");
for (String key: keys)
if(empty(key))
throw new IllegalArgumentException("Empty!");
// Rest of method...
return null;
}
With java 8 you can do:
public String normalizeList(List<String> keys) {
boolean bad = keys.stream().anyMatch(s -> (s == null || s.equals("")));
if(bad) {
//... do whatever you want to do
}
}
Aniket gave a really good answer for this as a comment:
if( keys.stream().anyMatch(String::isBlank) ) { .... String::isBlank is since Java11. –
Aniket Sahrawat
Apr 6, 2019 at 2:30
So to tweak your original solution using Aniket's comment:
public String normalize(List<String> keys) {
if(keys == null || keys.size() == 0 || keys.stream().anyMatch(String::isBlank)){
throw new IllegalArgumentException("Bad!");
}
// Rest of method...
}
if you can use org.springframework.util.CollectionUtils you can apply this
if(CollectionUtils.isEmpty(coll) || CollectionUtils.containsInstance(coll, null)) {
// throw ...;
}
It does not throw NullPointerException for immutable collection as List.of().
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.