本文主要是介绍版本排序,(如果 版本 是 1,1a,1.1a, 2, 2c , 1c , 1.2a, 3 , 5b , 5)进行排序,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
如果 版本 是 1,1a,1.1a, 2, 2c , 1c , 1.2a, 3 , 5b , 5
对上面的进行排序
利用 VersionComparator
导入依赖
<dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId><version>5.8.16</version>
</dependency>
代码块
import cn.hutool.core.comparator.VersionComparator;import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;public class Main {public static void main(String[] args) {List<String> originalList = new ArrayList<>();originalList.add("1");originalList.add("1a");originalList.add("1.1a");originalList.add("2");originalList.add("2c");originalList.add("1c");originalList.add("1.2a");originalList.add("3");originalList.add("5b");originalList.add("5");Map<String, String> versionMap = createVersionMap(originalList);List<String> transformedList = new ArrayList<>(versionMap.values());transformedList.sort(VersionComparator.INSTANCE);List<String> sortedOriginalList = applySortToOriginalList(originalList, transformedList, versionMap);System.out.println(sortedOriginalList);}private static Map<String, String> createVersionMap(List<String> originalList) {return originalList.stream().collect(Collectors.toMap(Function.identity(),Main::transformToVersionString,(v1, v2) -> v1, HashMap::new));}private static String transformToVersionString(String element) {return element;}private static List<String> applySortToOriginalList(List<String> originalList, List<String> sortedTransformedList, Map<String, String> versionMap) {return sortedTransformedList.stream().map(versionMap::get).collect(Collectors.toList());}
}
输出结果
[1, 1.1a, 1.2a, 1a, 1c, 2, 2c, 3, 5, 5b]
这篇关于版本排序,(如果 版本 是 1,1a,1.1a, 2, 2c , 1c , 1.2a, 3 , 5b , 5)进行排序的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!