How to sort names according to their initials in Java
How to sort according to the initials in Java? for this question, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible way.
/ * * @ Author:xyl * @ Date: 16:47 * @ Description on 2019-8-24: initials * / public static Map sort (List list) {Map rtMap = new HashMap (); Map result=new LinkedHashMap (); for (String s: list) {char [] arr = s.toCharArray (); HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat () DefaultFormat.setCaseType (HanyuPinyinCaseType.UPPERCASE); defaultFormat.setToneType (HanyuPinyinToneType.WITHOUT_TONE); if (arr [0] > 128) {try {String [] temp = PinyinHelper.toHanyuPinyinStringArray (arr [0], defaultFormat) If (temp! = null) {if (rtMap.containsKey (String.valueOf (temp [0] .charat (0) {rtMap.get (String.valueOf (temp [0] .charat (0) .add (s);} else {List li=new ArrayList () Li.add (s); rtMap.put (String.valueOf (temp [0] .charat (0)), li);} catch (BadHanyuPinyinOutputFormatCombination e) {e.printStackTrace () }} else {if (rtMap.containsKey (String.valueOf (arr [0])) {rtMap.get (String.valueOf (arr [0])) .add (s);} else {List li=new ArrayList (); li.add (s) RtMap.put (String.valueOf (arr [0]), li) } result = rtMap.entrySet () .stream () .sorted (Map.Entry.comparingByKey ()) .Collectors.toMap (Map.Entry::getKey, Map.Entry::getValue, (oldValue, newValue)-> oldValue, LinkedHashMap::new));} return result } this is the answer to the question about how to sort according to the initials in Java. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.