StringUtils.java
package practice;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.stream.Collectors;
/**
* Utils
*
* @author me
*
*/
public class Practice {
/** String list */
private static final List<String> INTEGER_LIST = Arrays.asList(null, "", " ", "0", "1", "2");
/**
* main
*
* @param args
*/
public static void main(String[] args) {
try {
run(INTEGER_LIST);
} catch (NumberFormatException e) {
echo.accept("・ Ω ・ v");
}
}
/**
* run
*
* @param list <E> The element type of this list.
* @throws NumberFormatException if conversion to numeric number is impossible.
*/
private static void run(List<String> list) throws NumberFormatException {
echo.accept(list.stream().filter(notBlank).map(Integer::parseInt)
.collect(Collectors.summingInt(Integer::intValue)));
}
/** not null */
static Predicate<String> notNull = v -> Optional.ofNullable(v).isPresent();
/** not space */
static Predicate<String> notSpace = v -> !v.equals(" ");
/** StringUtils.isBlank */
static Predicate<String> notBlank = v -> notNull.test(v) && !v.isEmpty()
&& Arrays.asList(v.split("")).stream().anyMatch(notSpace);
/** out */
static Consumer<Object> echo = v -> System.out.println(v);
}
3
Even if you can make your own, it's faster to add the library. But if it ends in a few lines, you don't have to use it one by one. I feel like I can solve anything because java already exists. I haven't seen apache.common.lang at all recently ...
Recommended Posts