[JAVA] I made StringUtils.isBlank

I made it

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);

}

result

3

Conclusion

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

I made StringUtils.isBlank
I made roulette in Java.
I made a chat app.
Ruby: I made a FizzBuzz program!
I made a shopify app @java
I made a GUI with Swing
I made a simple recommendation function.
I made an annotation in Java.
I made Mugen Kura Sushi Gacha
I made a matching app (Android app)
I made a package.xml generation tool.
[Android] I made a pedometer app.
[Ruby] I made a simple Ping client
I made various Fibonacci sequence functions (Ruby)
I made an eco server with scala
I made a risky die with Ruby
I made a plugin for IntelliJ IDEA
I made a rock-paper-scissors app with kotlin
I made a calculator app on Android
I made a new Java deployment tool
I made a rock-paper-scissors app with android
I made a bulletin board using Docker 1
I made a Diff tool for Java files
I made a primality test program in Java
04. I made a front end with SpringBoot + Thymeleaf
I made a mosaic art with Pokemon images
I made an app for myself! (Reading management app)
I made a gender selection column with enum
I made blackjack with Ruby (I tried using minitest)
I made an Android app for MiRm service
I made a rock-paper-scissors game in Java (CLI)
I made a viewer app that displays a PDF
I made a Docker container to run Maven
I made a Ruby extension library in C
[Rails] I made a draft function using enum
I made an API client for Nature Remo
I made a LINE bot with Rails + heroku
I made a portfolio with Ruby On Rails