我有这样的输入数据:
TRectAdvancedVO
AccountDTO
SVGMindMapVO
TransferDTO
TreeViewDAO
UMLDTO
UniqueVO
TRectAdvanced
SVGConstants
TransferService
AccountController
MindMapDemo
SVGMindMapTool
TreeViewAdvancedTool
AccountService
TransferController
TreeViewTool
MindMapRectUtils
SVGUtils
TNodeAdvanced
TreeRectUtils
UMLSVGUtils
UniqueList
我应该按类型(Service、Controller、DAO、Utils、List、Tool...)分隔类的名称。
输出顺序并不重要。我该怎么做?
输出:
TRectAdvancedVO
SVGMindMapVO
UniqueVO
AccountDTO
TransferDTO
UMLDTO
TreeViewDAO
TRectAdvanced
TNodeAdvanced
SVGConstants
TransferService
AccountService
AccountController
TransferController
MindMapDemo
SVGMindMapTool
TreeViewAdvancedTool
TreeViewTool
MindMapRectUtils
SVGUtils
TreeRectUtils
UMLSVGUtils
UniqueList
这里有一个 Java 版本来完成您的任务:
String components = """
TRectAdvancedVO
AccountDTO
SVGMindMapVO
TransferDTO
TreeViewDAO
UMLDTO
UniqueVO
TRectAdvanced
SVGConstants
TransferService
AccountController
MindMapDemo
SVGMindMapTool
TreeViewAdvancedTool
AccountService
TransferController
TreeViewTool
MindMapRectUtils
SVGUtils
TNodeAdvanced
TreeRectUtils
UMLSVGUtils
UniqueList
UnknownDummy
""";
List<String> types = List.of("VO", "DTO", "DAO", "Advanced", "Constants", "Service", "Controller", "Demo", "Tool", "Utils", "List");
Map<String, List<String>> groups = Arrays.stream(components.split("\\R"))
.collect(Collectors.groupingBy(component -> types.stream()
.filter(component::endsWith)
.findFirst()
.orElse("unknown")));
groups.forEach((groupName, groupMembers) -> {
System.out.printf("Type: %s\n", groupName);
groupMembers.forEach(component -> System.out.printf("\t%s\n", component));
System.out.println();
});