How to efficiently remove all null elements from a ArrayList or String Array in java?

How to efficiently remove all null elements from a ArrayList or String Array in java?

To efficiently remove all null elements from an ArrayList or a String array in Java, you can use the following approaches:

For ArrayList:

  1. Using Java Streams (Java 8+): You can use Java Streams to filter out the null elements and create a new ArrayList containing only non-null elements.

    import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; public class RemoveNullFromArrayList { public static void main(String[] args) { List<String> list = new ArrayList<>(); list.add("A"); list.add(null); list.add("B"); list.add(null); list.add("C"); list = list.stream() .filter(element -> element != null) .collect(Collectors.toList()); System.out.println(list); } } 

    In this example, filter() is used to exclude null elements from the original list.

  2. Using an Iterator (pre-Java 8): If you are not using Java 8 or later, you can use an Iterator to iterate through the ArrayList and remove null elements.

    import java.util.ArrayList; import java.util.Iterator; import java.util.List; public class RemoveNullFromArrayList { public static void main(String[] args) { List<String> list = new ArrayList<>(); list.add("A"); list.add(null); list.add("B"); list.add(null); list.add("C"); Iterator<String> iterator = list.iterator(); while (iterator.hasNext()) { if (iterator.next() == null) { iterator.remove(); } } System.out.println(list); } } 

For String Array:

  1. Using Java Streams (Java 8+): If you have a String array and want to remove null elements, you can convert the array to a List<String>, apply the same stream-based filtering, and then convert it back to an array.

    import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; public class RemoveNullFromStringArray { public static void main(String[] args) { String[] array = {"A", null, "B", null, "C"}; List<String> list = Arrays.asList(array); list = list.stream() .filter(element -> element != null) .collect(Collectors.toList()); array = list.toArray(new String[0]); System.out.println(Arrays.toString(array)); } } 
  2. Using a Loop (pre-Java 8): If you are not using Java 8 or later, you can create a new array and iterate through the original array, copying non-null elements to the new array.

    public class RemoveNullFromStringArray { public static void main(String[] args) { String[] array = {"A", null, "B", null, "C"}; int nonNullCount = 0; for (String element : array) { if (element != null) { nonNullCount++; } } String[] newArray = new String[nonNullCount]; int newIndex = 0; for (String element : array) { if (element != null) { newArray[newIndex++] = element; } } System.out.println(Arrays.toString(newArray)); } } 

Choose the method that best suits your needs and is compatible with your Java version. Streams provide a concise and expressive way to filter elements, while using an Iterator or a loop can be more efficient for large collections in some cases.


More Tags

http-status-code-405 wkwebview iphone-sdk-3.0 variable-substitution constructor jpanel action formview vnc elasticsearch-aggregation

More Java Questions

More Chemical reactions Calculators

More Livestock Calculators

More Tax and Salary Calculators

More Everyday Utility Calculators