Java – empty array is an empty list
•
Java
Arrays. Aslist (E [] e) returns the view of the array as a list, but it throws a NullPointerException when the array is empty
Arrays.asList(null); //NullPointerException.
Actually, I'm doing it
List list = possibleNullArray != null ? Arrays.asList(possibleNullArray) : Collections.EMPTY_LIST;
However, creating a utility class in my project is just for this purpose, and I don't like it Are there any utility classes or libraries like Apache commons or guava that convert empty arrays to empty lists? (i.e. zero security converter between array and collection)
How do you solve this problem?
Solution
I didn't know that any util method in Apache commons / guava would create an empty list instance
The best thing you can do is pre initialize possible empty arrays, such as with arrayutils nullToEmpty(). Get rid of null as soon as possible
SomeObject[] array = ArrayUtils.nullToEmpty(possiblyNullArray);
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码