lambda過濾空值、轉字符串

List<String> tagsList = Arrays.asList("a", "b", null);
//過濾掉空值
List<String> listWithoutNulls = tagsList.stream()
        .filter(Objects::nonNull)
        .collect(Collectors.toList());
//過濾空值而且逗號分隔轉字符串
String tags = tagsList.stream()
        .filter(Objects::nonNull)
        .collect(Collectors.joining(","));
System.out.println(tags);
複製代碼
相關文章
相關標籤/搜索