[JAVA] What kind of StringUtil is good

I want a method that can do anything with this

do not want?

What kind of thing is good

I can't help if the type is different You can safely handle anything related to String For the purpose of

I will try it for the time being

StringUtils.java


package utils;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.Arrays;
import java.util.HashMap;

/**
 *If it's a String, leave it to me
 * 
 * @author Mr.Extreme versatility
 *
 */
public class StringUtils {

	/**Multiple string arguments*/
	private static final List<String> MULTIPLE_STRING = Arrays.asList("a", null, "b", "", "3");

	/**
	 *experimental
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		System.out.println(extraction(MULTIPLE_STRING));
	}

	/**
	 * {@code null}To blank.<br>
	 * {@code null}As it is not.
	 * 
	 * @param str Input string
	 * @return conversion result
	 */
	private static String nullToBlank(String str) {
		return Objects.toString(str, "");
	}

	/**
	 * {@code null}To blank.<br>
	 * {@code null}As it is not.
	 * <p>
	 *The argument itself{@code null}Or when the number of elements is 0,Returns an empty list.
	 * 
	 * @param list input{@code List<String>}
	 * @return conversion result
	 */
	private static List<String> nullToBlank(List<String> list) {
		if (list != null)
			return list.stream().map(StringUtils::nullToBlank).collect(Collectors.toList());
		return Arrays.asList();
	}

	/**
	 *Entered{@code List<String>}In<br>
	 * {@code null}And eliminate the empty string
	 * 
	 * @param list input{@code List<String>}
	 * @return conversion result
	 */
	private static List<String> nullBlankSmasher(List<String> list) {
		return nullToBlank(list).stream().filter(v -> !v.isEmpty()).collect(Collectors.toList());
	}

	/**
	 * {@code Map<String, Integer>}Against
	 * <p>
	 * <ul>
	 * <li>When the entered character string can be converted to a numerical value<br>
	 * {Empty string,Numerical value after conversion}return it<br>
	 * <li>When the entered character string cannot be converted to a numerical value<br>
	 * {Character string before conversion, {@code null}}return it
	 * </ul>
	 * 
	 * @param map Map for storage
	 * @param str Numeric conversion target character string
	 */
	private static void store(Map<String, Integer> map, String str) {
		try {
			map.put("", Integer.parseInt(str));
		} catch (NumberFormatException e) {
			map.put(str, null);
		}
	}

	/**
	 *Convert what can be converted to numerical values, and sort those that cannot be converted unchanged.
	 * <p>
	 * <ul>
	 * <li>When the entered character string can be converted to a numerical value<br>
	 * {Empty string,Numerical value after conversion}return it<br>
	 * <li>When the entered character string cannot be converted to a numerical value<br>
	 * {Character string before conversion, {@code null}}return it
	 * </ul>
	 * 
	 * @param list input{@code List<String>}
	 * @return Sorting result
	 */
	private static Map<String, Integer> toInteger(List<String> list) {
		Map<String, Integer> map = new HashMap<String, Integer>();
		nullBlankSmasher(list).forEach(v -> {
			store(map, v);
		});
		return map;
	}

	/**
	 *Perform the following operations on the list of entered character strings
	 * <p>
	 * <ul>
	 * <li>{@code null}And remove the empty string
	 * <li>Convert a character string that can be converted to a number to a number
	 * <li>With a list of strings that could not be converted,Returns a map of the list of numbers after conversion
	 * </ul>
	 * 
	 * @param list input{@code List<String>}
	 * @return Sorting result
	 */
	private static Map<List<String>, List<Integer>> extraction(List<String> list) {
		Map<List<String>, List<Integer>> map = new HashMap<List<String>, List<Integer>>();
		map.put(toInteger(list).keySet().stream().filter(v -> !v.isEmpty()).collect(Collectors.toList()),
				toInteger(list).values().stream().filter(Objects::nonNull).collect(Collectors.toList()));
		return map;
	}

}

what's this

I made it

Input value: List Output value; Map <List , List > processing: If any element in the list of input character strings can be converted to a numerical value, it is converted to a numerical value. Leave what cannot be converted Separate into a list of strings and a list of numbers and store them in the map

What I was conscious of

Correspondence that input value is null and the number of elements is 0 Do not make it all at once, but make it separately for each part Made it possible to handle when you want to use only a part

Bad place

I want to make it a more general window by using Object type and Optional

Well it's difficult ...

I want a panacea

Recommended Posts

What kind of StringUtil is good
What kind of method is define_method?
'% 02d' What is the percentage of% 2?
What is it? ~ 3 types of "no" ~
Road to Java Engineer Part2 What kind of language is Java?
What is testing? ・ About the importance of testing
What is the data structure of ActionText?
What is Cubby
What is Docker?
What is null? ]
What is Keycloak
What is maven?
What is Jackson?
What is Docker
What is self
What is Jenkins
What is ArgumentMatcher?
What is IM-Juggling?
What is params
What is SLF4J?
What is Facade? ??
What is Java <>?
What is Gradle?
What is POJO
What is Java
What is centOS
What is RubyGem?
What is programming?
What is before_action?
What is Docker
What is Byte?
What is Tomcat
What is JSP? ~ Let's know the basics of JSP !! ~
What is Maven Assembly?
Existence check of has_many and belongs_to-optional: What is true?
What is `docker-compose up`?
What is a constructor?
What is vue cli
What is an interface?
The comparison of enums is ==, and equals is good [Java]
What is Ruby's self?
What is hard coding?
What is a stream
What is Ruby's attr_accessor?
What is Java Encapsulation?
What is permission denied?
What is instance control?
What is an initializer?
What is Spring Tools 4
What is an operator?
What is object orientation?
What is Guava's @VisibleForTesting?
What is MVC model?
What is an annotation?
What is Java technology?
What is Java API-java
What is @ (instance variable)?
What is Gradle's Artifact?
What is JPA Auditing?
[Swift] What is dismiss?
[Java] What is flatMap?