TreeNode (Set<MavenArchetype> archetypes) { List<MavenArchetype> list = new ArrayList<>(archetypes); list.sort((o1, o2) -> { String key1 = o1.groupId + ":" + o1.artifactId; String key2 = o2.groupId + ":" + o2.artifactId; int result = key1.compareToIgnoreCase(key2); if (result != 0) return result; return o2.version.compareToIgnoreCase(o1.version); }); Map<String, List<MavenArchetype>> map = new TreeMap<>(); for (MavenArchetype each : list) { String key = each.groupId + ":" + each.artifactId; List<MavenArchetype> versions = map.get(key); if (versions == null) { versions = new ArrayList<>(); map.put(key, versions); } versions.add(each); } DefaultMutableTreeNode result = new DefaultMutableTreeNode("root", true); for (List<MavenArchetype> each : map.values()) { MavenArchetype eachArchetype = each.get(0); DefaultMutableTreeNode node = new DefaultMutableTreeNode(eachArchetype, true); for (MavenArchetype eachVersion : each) { DefaultMutableTreeNode versionNode = new DefaultMutableTreeNode(eachVersion, false); node.add(versionNode); } result.add(node); } return result; }