LoginSignup
0
0

StringUtils.isBlank作ってみた

Last updated at Posted at 2019-10-20

##つくってみました

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

##結論
なんだかんだ自作できてもライブラリ追加した方が早くね問題
けど数行で終わるならいちいち使わなくてもいいですよね
ぶっちゃけもうjava既存のでなんでも解決できそうな気分になってます
最近apache.common.lang全然見ない...

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0