String joined = Stream.of(val1, val2, val3, val4) .filter(s -> s != null && !s.isEmpty()) .collect(Collectors.joining(","));