很多项目中,需要用到中文名称到首字母进行分组,例如:城市、游戏等等。。。
- /**
- * 将集合数据按照汉字首字母分组排序
- *
- * @param list
- * @return
- */
- public Map<String, Object> screenManufacturer(List<Game> list) {
- Set<String> hasAddedNameSet = new HashSet<>();
- long start = System.currentTimeMillis();
- try {
- // 输出26个字母
- Map<String, Object> map = new TreeMap<>();
- int n = 0;
- for (int i = 1; i < 27; i++) {
- String word = String.valueOf((char) (96 + i)).toUpperCase();
- // 循环找出 首字母一样的数据
- List<Game> letter = new ArrayList<>();
- for (Iterator<Game> iter = list.listIterator(); iter.hasNext(); ) {
- Game game = iter.next();
- if (hasAddedNameSet.contains(game.getName())) {
- continue;
- }
- String pybf = PinyinUtil.getFullSpell(game.getName());
- String firstWord = pybf.substring(0, 1);
- if (word.equalsIgnoreCase(firstWord)) {
- hasAddedNameSet.add(game.getName());
- letter.add(game);
- }
- n++;
- }
- map.put(word, letter);
- }
- log.info("总共循坏了{}次", n);
- long end = System.currentTimeMillis();
- log.info("总耗时:{}毫秒", end - start);
- return map;
- } catch (Exception e) {
- e.printStackTrace();
- }
- return null;
- }
复制代码- import net.sourceforge.pinyin4j.PinyinHelper;
- import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
- import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
- import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
- import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
- import java.text.Collator;
- import java.util.*;
- public class PinyinUtil {
- public static void main(String[] args) {
- List<String> names = new ArrayList<>();
- names.add("张三");
- names.add("李四");
- names.add("王武");
- names.add("刘德华");
- names.add("精舍蓝军");
- Map<String, Object> stringObjectMap = screenManufacturer(names);
- System.out.println(stringObjectMap);
- }
- /**
- * 获取汉字串拼音,英文字符不变
- */
- public static String getFullSpell(String chinese) {
- StringBuffer pybf = new StringBuffer();
- char[] arr = chinese.toCharArray();
- HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
- defaultFormat.setCaseType(HanyuPinyinCaseType.UPPERCASE);
- defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
- for (int i = 0; i < arr.length; i++) {
- if (arr[i] > 128) {
- try {
- pybf.append(PinyinHelper.toHanyuPinyinStringArray(arr[i], defaultFormat)[0]);
- } catch (BadHanyuPinyinOutputFormatCombination e) {
- e.printStackTrace();
- }
- } else {
- pybf.append(arr[i]);
- }
- }
- return pybf.toString();
- }
- /**
- * 将集合数据按照汉字首字母分组排序
- */
- public static Map<String, Object> screenManufacturer(List<String> list) {
- try {
- Collator com = Collator.getInstance(Locale.CHINA);
- // 按字母排序
- Collections.sort(list, com);
- // 输出26个字母
- Map<String, Object> map = new TreeMap<>();
- for (int i = 1; i < 27; i++) {
- String word = String.valueOf((char) (96 + i)).toUpperCase();
- // 循环找出 首字母一样的数据
- List<String> letter = new ArrayList<>();
- for (String str : list) {
- String pybf = getFullSpell(str);
- String zm = pybf.substring(0, 1);
- if (word.equals(zm)) {
- letter.add(str);
- }
- }
- map.put(word, letter);
- }
- return map;
- } catch (Exception e) {
- e.printStackTrace();
- }
- return null;
- }
- }
复制代码 POM文件
- <!--拼音-->
- <dependency>
- <groupId>com.belerweb</groupId>
- <artifactId>pinyin4j</artifactId>
- <version>2.5.0</version>
- </dependency>
复制代码 天生的效果
- {
- "code": 200,
- "message": "操作成功",
- "data": {
- "A": [],
- "B": [],
- "C": [
- {
- "gameId": "g123",
- "name": "CS"
- }
- ],
- "D": [
- {
- "gameId": "g123",
- "name": "地下城勇士"
- },
- {
- "gameId": "g123",
- "name": "DNF"
- },
- {
- "gameId": "g123",
- "name": "DOTA"
- }
- ],
- "E": [],
- "F": [],
- "G": [],
- "H": [],
- "I": [],
- "J": [],
- "K": [],
- "L": [],
- "M": [
- {
- "gameId": "g123",
- "name": "魔兽世界"
- }
- ],
- "N": [],
- "O": [],
- "P": [],
- "Q": [],
- "R": [],
- "S": [
- {
- "gameId": "g123",
- "name": "守卫剑阁"
- }
- ],
- "T": [
- {
- "gameId": "7fbf48bb3204",
- "name": "test22"
- }
- ],
- "U": [],
- "V": [],
- "W": [
- {
- "gameId": "g123",
- "name": "王者荣耀"
- }
- ],
- "X": [],
- "Y": [],
- "Z": [
- {
- "gameId": "g123",
- "name": "诛仙"
- }
- ]
- }
- }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |