문자열 모음이 있는데 문자열 집합이 모두 비어 있거나 null로 변환하려고합니다. 문자열이 제거되고 다른 모든 문자열은 잘립니다.Guava : 필터와 변환을 결합하는 방법은 무엇입니까?
나는 두 단계를 수행 할 수 있습니다 한 번에 두 가지 작업을 결합의 구아바 방법
final List<String> tokens =
Lists.newArrayList(" some ", null, "stuff\t", "", " \nhere");
final Collection<String> filtered =
Collections2.filter(
Collections2.transform(tokens, new Function<String, String>(){
// This is a substitute for StringUtils.stripToEmpty()
// why doesn't Guava have stuff like that?
@Override
public String apply(final String input){
return input == null ? "" : input.trim();
}
}), new Predicate<String>(){
@Override
public boolean apply(final String input){
return !Strings.isNullOrEmpty(input);
}
});
System.out.println(filtered);
// Output, as desired: [some, stuff, here]
그러나이있다?
로 skaffman 노트; FluentIterable를 사용
, 당신은 이런 일을 할 수 있어야한다 꽤 일반적으로 사용되는 함수에 대한주의 사항은'Strings' API에 정적 함수''''함수''와''Predicate''를 추가하면됩니다. 유지 관리 담당자가 http://code.google.com/p/guava-libraries/issues/list에서 반응이 좋았습니다. – Carl
@ Carl 이전에 이미 파이프 라인에서 http://code.google.com/p/guava-libraries/issues/list?can=2&q=reporter:sean,mostlymagic.com에 문제가 있습니다. 원하지 않습니다. 그들의 신경에 타기. 하지만 결국은 구아바가 나를 대신하여 커먼즈/랭과 커먼스/아이오와를 대체하기를 원하기 때문에 그렇게 할 수도 있습니다. 그리고 여전히 그걸 갈 길이 멀지 요. –
필자는 필터를 적용한 다음 변환 할 수 있다고 생각합니다. 변환시 null case를 테스트 할 필요가 없습니다. –