Example Code

Java Array Utility methods – Trim/notNull for Array of Objects

1 Mins read

This is another simple but useful Java Array Utility/Util method, which can be used to trim/remove all null Object values in an array. Doing this would eliminate a null check on every array element. This method returns null value if the input array itself is null. We can call this as trim of array, as it will return all not null objects in the result(trimming the size of array).

* This Array utility or util method can be used to trim all
* the null values in the array.
* For input {"1", "2", null, "3","4"}
* the output will be {"1", "2", "3","4"}
* @param values
* @return Array of Object
public static Object[] notNull(final Object[] values) {
if (values == null) {
return null;
ArrayList newObjectList = new ArrayList();
for (int i = 0, length = values.length; i < length; i++) {
if (values[i] != null) {
return newObjectList.toArray();

A notNull method for simple object is already a popular utility method, which is used by most programmers now. Checking Array with the same notNull can also be good idea. I am not sure how much performance boost we can get by doing this but I am open to thoughts on it. Please share your comment with us.

Related posts
.NETExample CodeProgramming

Custom CRM Search Application in MVC

32 Mins read
Today, I will explain how to create advanced search using MVC application same as CRM Advanced Search. For that first you have…
CheatsheetExample CodeExcelTutorials

15 Useful Excel Formula Cheat Sheet

5 Mins read
Looking for list of excel formulas? We have a create a cheat sheet for excel formulas to help beginners. The excel Spreadsheet…
Example CodeJavaRegEx

How To Use Regular Expressions In Java

3 Mins read
Have you ever found yourself needing to search through a string and check whether a condition is true or not? For instance,…
Power your team with InHype

Add some text to explain benefits of subscripton on your services.

Leave a Reply

Your email address will not be published. Required fields are marked *