do not want?
I can't help if the type is different You can safely handle anything related to String For the purpose of
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;
}
}
I made it
Input value: List
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
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