Group JAVA 根据字符串键中的标记映射键

Group JAVA Map keys based on tokens in string key

我有这样的要求,我需要解析一个文本文件并从中提取 n-gram,并将 n-gram 映射及其计数存储在地图中。现在,映射键是字符串,其中可以包含 1,2,3 个单词。

e.g. ("mango", 10), ("facbook friend", 6), ("the rich guy", 3) 1<=n<=3

映射示例:

("mango", 2)

("apple", 1)

("mango tree", 5)

("facebook friend", 3)

("facebook people", 8)

("Bougth new watch", 2)

现在,我想根据地图键中的关键字标记长度对地图进行排序。就像所有 1 字键映射应该在映射中首先出现,然后是 2 个字,然后是 3 个字映射。

我尝试使用 TreeMap,但主要的挑战是为排序顺序定义 compareTo 函数。有任何想法吗?像下面的方法不起作用。

    Map<String, Integer> m = new TreeMap<>(Comparator.comparingInt(k -> k.split(" ").length));

    m.put("mango tree", 5);
    m.put("Bought new watch", 2);
    m.put("apple", 1);
    m.put("mango tree", 5);
    m.put("Bought new watch", 2);
    m.put("appl1", 1);
    m.put("mango 1", 5);
    m.put("Bought 1 watch", 2);
    m.put("appl2", 1);
    m.put("mango 2", 5);
    m.put("Bought 2 watch", 2);
    m.put("appl3", 1);
    System.out.println(m);

输出:{apple=1, mango tree=5, Bought new watch=2}

您可以使用 Collectors.toMap 与订购的地图供应商一起使用,如下所示:

Map<String, Integer> m = new HashMap<>();
m.put("mango tree", 5);
m.put("Bought new watch", 2);
m.put("apple", 1);

LinkedHashMap<String, Integer> sortedMap = m.entrySet().stream()
        .sorted(Comparator.comparingInt(e -> e.getKey().split(" ").length))
        .collect(Collectors.toMap(Map.Entry::getKey,
                Map.Entry::getValue,
                (o1, o2) -> o1,
                LinkedHashMap::new));

System.out.println(sortedMap);

输出

{apple=1, mango tree=5, Bought new watch=2}

您还可以使用以下 .sorted(... 行:

.sorted(Map.Entry.comparingByKey(Comparator.comparingInt(k -> k.split(" ").length)))

以下代码按顺序插入记录。

    SortedMap<String, Integer> m = new TreeMap<>(new Comparator<String>() {
        @Override
        public int compare(String s1, String s2) {
            int s2length = s2.split(" ").length;
            int s1length = s1.split(" ").length;
            return s2length>s1length?-1:s2length==s1length && s2.equals(s1)?0:1;
        }
    });

    m.put("mango tree", 5);
    m.put("you have to check this out too", 1);
    m.put("apple", 1);
    m.put("apple", 5);
    m.put("you have to check this out", 1);
    m.put("check this out", 1);
    m.put("Bought new watch", 2);
    m.put("check this out too", 1);

    System.out.println(m);