Java 如何根据密钥长度对映射进行排序

Java 如何根据密钥长度对映射进行排序,java,sorting,java-stream,Java,Sorting,Java Stream,我有这张地图: Map < String, String > unsortMap = new HashMap < String, String > (); unsortMap.put("./cq:dialog/content/items/tabs/items/tab1/items/columns/items", "40"); unsortMap.put("./cq:dialog/content", "8

我有这张地图:

Map < String, String > unsortMap = new HashMap < String, String > ();
unsortMap.put("./cq:dialog/content/items/tabs/items/tab1/items/columns/items", "40");
unsortMap.put("./cq:dialog/content", "80");
unsortMap.put("./cq:dialog", "75");
unsortMap.put("./cq:dialog/content/items/tabs/items/tab2/items/columns/items", "40");
unsortMap.put("./cq:dialog/content/sling:resourcetype", "granite/ui/components/coral/foundation/container");
为此,我这样写:

Map<String, String> sortedMap = unsortMap.entrySet().stream()
       .sorted(Map.Entry.comparingByKey())
       .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue,
                        (oldValue, newValue) -> oldValue, LinkedHashMap::new));
Map sortedMap=unsortMap.entrySet().stream()
.sorted(Map.Entry.comparingByKey())
.collect(Collectors.toMap(Map.Entry::getKey,Map.Entry::getValue,
(oldValue,newValue)->oldValue,LinkedHashMap::new));

它没有按预期给出输出,因为它没有基于“/”进行比较。我尝试了自定义比较器,但未能获得预期结果。

要仅比较长度,我们可以在自定义比较器中使用
整数。比较


为了比较斜杠的数量,我们可以过滤两个字符串的字符

final NavigableMap<String, String> sortedMap = new TreeMap<>((a, b) -> {
    final long slashes1 = a.chars().filter(c -> c == '/').count();
    final long slashes2 = b.chars().filter(c -> c == '/').count();
    return slashes1 != slashes2 ? Long.compare(slashes1, slashes2) : a.compareTo(b);
});
sortedMap.putAll(unsortMap);
或者:

Map<String, String> sortedMap = unsortMap.entrySet().stream()
        .sorted((a, b) -> Long.compare(a.getKey().chars().filter(c->c=='/').count(), b.getKey().chars().filter(c->c=='/').count()))
        .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (oldValue, newValue) -> oldValue,
                LinkedHashMap::new));
Map sortedMap=unsortMap.entrySet().stream()
.sorted((a,b)->Long.compare(a.getKey().chars().filter(c->c=='/')).count(),b.getKey().chars().filter(c->c=='/').count())
.collect(Collectors.toMap(Map.Entry::getKey,Map.Entry::getValue,(oldValue,newValue)->oldValue,
LinkedHashMap::new));

如果键的“/”数较少,那么它将位于顶部,这怎么解决呢?@Naman你说得对。我误解了这个问题。我要编辑了,我也在想同样的事情。但它还是被接受了。也许OP不知道他们想要什么。你可以使用
newtreemap(Comparator.comparingLong((String s)->s.chars().filter(c->c=='/').count())来缩短它。然后使用Comparator.naturalOrder()进行比较
@Andreas感谢您的建议。您只想根据键的长度或键包含的斜杠数进行比较吗?问题有点不清楚。如何计算用例——如果键的“/”数较少,那么它将位于顶部?根据公认的答案,它甚至不符合您提出的要求。试着弄清楚你在寻找什么。一旦你弄清楚了这一点,当你有两个类似的条目时,你会想到它们中的
/
计数吗?e、 g.
/cq:dialog/content/
/cq:dialog/content/sling:resourcetype
顺序中哪个在前?@Naman,/cq:dialog/content/顺序中在前
final NavigableMap<String, String> sortedMap = new TreeMap<>(
        (a, b) -> a.length() != b.length() ? Integer.compare(a.length(), b.length()) : a.compareTo(b));
sortedMap.putAll(unsortMap);
final NavigableMap<String, String> sortedMap = new TreeMap<>((a, b) -> {
    final long slashes1 = a.chars().filter(c -> c == '/').count();
    final long slashes2 = b.chars().filter(c -> c == '/').count();
    return slashes1 != slashes2 ? Long.compare(slashes1, slashes2) : a.compareTo(b);
});
sortedMap.putAll(unsortMap);
final NavigableMap<String, String> sortedMap = new TreeMap<>(
        Comparator.comparingLong((String s) -> s.chars().filter(c -> c == '/').count())
         .thenComparing(Comparator.naturalOrder()));
Map<String, String> sortedMap = unsortMap.entrySet().stream()
        .sorted((a, b) -> Long.compare(a.getKey().chars().filter(c->c=='/').count(), b.getKey().chars().filter(c->c=='/').count()))
        .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (oldValue, newValue) -> oldValue,
                LinkedHashMap::new));