今天小黄黄又来找我了
小黄:
我有个需求, 有以下字符串
EUR 39, EU 39, 38, 2, AUS 38, UK 38, US 37, US 37, US 36, UK 36, UK 35, UK 12/18, UK 12-14, UK 12/21, UK 8/18, UK 8-10
我想要分组, 想要以下结果:
很好的需求, easy, 使用 #feilong-core# 包 写一个,正好活跃下大脑
大致步骤:
分组注意点:
这里使用 feilong 包很有用的方法com.feilong.core.util.CollectionsUtil.group(Iterable<String>, Transformer<String, String>)
具体的代码实现:
class="pl-c" style="color: #6a737d;">/** * 尺码中 支持的符号. 目前由的格式有 UK 12-14, UK 12/21. */ public static final List<String> SEPARATOR_LIST = toList("-", "/"); //--------------------------------------------------------------- public static void main(String[] args){ String str = "EUR 39, EU 39, 38, 2, AUS 38, UK 38, US 37, US 37, US 36, UK 36, UK 35, UK 12/18, UK 12-14, UK 12/21, UK 8/18, UK 8-10"; Map<String, List<String>> map = group(str); //--------------------------------------------------------------- for (Map.Entry<String, List<String>> entry : map.entrySet()){ List<String> value = entry.getValue(); System.out.println(value); } } //--------------------------------------------------------------- /** * 分组 * @param str */ private static Map<String, List<String>> group(String str){ String[] tokenizeToStringArray = StringUtil.tokenizeToStringArray(str, ","); SortUtil.sortArray(tokenizeToStringArray); List<String> list = toList(tokenizeToStringArray); //--------------------------------------------------------------- return CollectionsUtil.group(list, new Transformer<String, String>(){ @Override public String transform(String input){ //数字单独放 if (CharUtils.isAsciiNumeric(input.charAt(0))){ return "Number"; } //--------------------------------------------------------------- //字母 以空格分隔 String country = input.split(SPACE)[0]; //包含 - 或者 / for (String separator : SEPARATOR_LIST){ String value = input.split(SPACE)[1]; if (value.contains(separator)){ return country + separator + value.split(separator)[0]; } } return country; } }); }
** 输出 :**
[2, 38] [AUS 38] [EU 39] [EUR 39] [UK 12-14] [UK 12/18, UK 12/21] [UK 35, UK 36, UK 38] [UK 8-10] [UK 8/18] [US 36, US 37, US 37]
maven 依赖
<project> .... <properties> <version.feilong-platform>1.12.10</version.feilong-platform> .... </properties> .... <repositories> <repository> <id>feilong-repository</id> <url>https://raw.github.com/venusdrogon/feilong-platform/repository</url> </repository> </repositories> .... <dependencies> .... <dependency> <groupId>com.feilong.platform</groupId> <artifactId>feilong-util-all</artifactId> <version>${version.feilong-platform}</version> </dependency> .... </dependencies> .... </project>
~ 完 ~