java - Join comma if not empty or null

Java - Join comma if not empty or null

To join strings with a comma in Java while skipping null or empty strings, you can use a combination of Java 8's Stream API and String.join() method. Here's how you can do it:

import java.util.Arrays; import java.util.stream.Collectors; public class Main { public static void main(String[] args) { // Sample strings String[] strings = {"apple", "", null, "banana", null, "orange"}; // Join strings with comma, skipping null or empty strings String result = Arrays.stream(strings) .filter(s -> s != null && !s.isEmpty()) .collect(Collectors.joining(", ")); System.out.println(result); } } 

In this example:

  • We use Arrays.stream() to convert the array of strings into a Stream.
  • We use .filter() to exclude null or empty strings from the Stream.
  • Finally, we use .collect(Collectors.joining(", ")) to join the remaining strings with a comma and space.

This will print: apple, banana, orange, skipping any null or empty strings.

Examples

  1. Java join comma-separated string if not empty or null

    • Description: Describes how to join strings with commas in Java, excluding null or empty strings.
    // Join non-null and non-empty strings with commas using Java 8 Stream API String result = String.join(", ", myList.stream().filter(s -> s != null && !s.isEmpty()).collect(Collectors.toList())); 
  2. Join strings with comma if not null or empty

    • Description: Illustrates a method to concatenate strings with commas, excluding null or empty strings.
    // Join strings with comma if not null or empty using StringBuilder StringBuilder sb = new StringBuilder(); for (String str : myArray) { if (str != null && !str.isEmpty()) { if (sb.length() > 0) { sb.append(", "); } sb.append(str); } } String result = sb.toString(); 
  3. Java join comma-separated list ignoring null or empty values

    • Description: Provides a solution to concatenate a list of strings with commas, ignoring null or empty values.
    // Join comma-separated list ignoring null or empty values using Apache Commons Lang library String result = StringUtils.join(myList.stream().filter(StringUtils::isNotEmpty).toArray(), ", "); 
  4. Join strings with comma Java 8 if not null or empty

    • Description: Demonstrates how to join strings with commas in Java 8, excluding null or empty strings.
    // Join strings with comma if not null or empty using Java 8 Stream API String result = myList.stream().filter(s -> s != null && !s.isEmpty()).collect(Collectors.joining(", ")); 
  5. Concatenate strings with comma separator ignoring null or empty

    • Description: Explains how to concatenate strings with commas, ignoring null or empty strings.
    // Concatenate strings with comma separator ignoring null or empty using StringBuilder StringBuilder sb = new StringBuilder(); for (String str : myArray) { if (str != null && !str.isEmpty()) { if (sb.length() > 0) { sb.append(", "); } sb.append(str); } } String result = sb.toString(); 
  6. Java concatenate strings with comma if not empty

    • Description: Shows how to concatenate strings with commas, excluding empty strings.
    // Concatenate strings with comma if not empty using Java 8 Stream API String result = myList.stream().filter(s -> !s.isEmpty()).collect(Collectors.joining(", ")); 
  7. Join comma-separated values ignoring null or empty strings

    • Description: Guides on joining comma-separated values, excluding null or empty strings.
    // Join comma-separated values ignoring null or empty strings using Apache Commons Lang library String result = StringUtils.join(ArrayUtils.removeElements(myArray, null, ""), ", "); 
  8. Java concatenate strings with comma separator if not null or empty

    • Description: Provides a method to concatenate strings with commas, excluding null or empty strings.
    // Concatenate strings with comma separator if not null or empty using StringBuilder StringBuilder sb = new StringBuilder(); for (String str : myArray) { if (str != null && !str.isEmpty()) { if (sb.length() > 0) { sb.append(", "); } sb.append(str); } } String result = sb.toString(); 
  9. Join non-null strings with comma separator in Java

    • Description: Explains how to join non-null strings with commas in Java.
    // Join non-null strings with comma separator using Java 8 Stream API String result = myList.stream().filter(Objects::nonNull).collect(Collectors.joining(", ")); 
  10. Java join comma-separated values if not null or empty

    • Description: Demonstrates a method to join comma-separated values, excluding null or empty strings.
    // Join comma-separated values if not null or empty using Java 8 Stream API String result = myList.stream().filter(s -> s != null && !s.isEmpty()).collect(Collectors.joining(", ")); 

More Tags

javax.crypto aws-lambda python-2.7 stress-testing laravel-queue strlen wave google-font-api jaxb2 adapter

More Programming Questions

More Everyday Utility Calculators

More Other animals Calculators

More Various Measurements Units Calculators

More Genetics Calculators