Java Array Empty Check removeEmptyEntries(String[] array)

Here you can find the source of removeEmptyEntries(String[] array)

Description

remove Empty Entries

License

Apache License

Declaration

public static String[] removeEmptyEntries(String[] array) 

Method Source Code


//package com.java2s;
/*//from  ww  w.ja  va2s  .c  o m
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

import java.util.ArrayList;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Main {
    public static String[] removeEmptyEntries(String[] array) {
        if (array == null || array.length == 0) {
            return array;
        }

        List<String> result = new ArrayList<String>();
        for (String entry : array) {
            entry = removeEmptyEntry(entry);

            if (entry != null) {
                result.add(entry);
            }
        }

        if (result.size() == 0) {
            return null;
        }

        return result.toArray(new String[result.size()]);
    }

    public static String[] removeEmptyEntries(String[] array, String[] defaultArray) {
        String[] result = removeEmptyEntries(array);
        if (result == null) {
            return defaultArray;
        } else {
            return result;
        }
    }

    public static Map<String, String> removeEmptyEntries(Map<String, String> map) {
        if (map == null) {
            return null;
        }

        Map<String, String> result = new HashMap<String, String>();
        for (Map.Entry<String, String> entry : map.entrySet()) {
            String key = removeEmptyEntry(entry.getKey());
            String value = removeEmptyEntry(entry.getValue());

            if (key != null && value != null) {
                result.put(key, value);
            }
        }

        if (result.size() == 0) {
            return null;
        }

        return result;
    }

    public static String removeEmptyEntry(String entry) {
        if (entry == null) {
            return null;
        }

        entry = entry.trim();

        if (entry.length() == 0) {
            return null;
        }

        return entry;
    }
}

Related

  1. isNullOrEmptyOrFirstElementBlank(String[] values)
  2. normalizeTokens(String[] tokens, boolean removeEmpty)
  3. nullOrEmpty(Object[] array)
  4. removeEmpty(String[] strings)
  5. removeEmptyElements(String[] firstArray)
  6. removeEmptyLines(String lines[])
  7. removeEmptyString(final String[] words)
  8. removeEmptyString(String[] strOrigin)
  9. removeEmptyStrings(String[] data)