我有这个要求,我需要解析一个文本文件并从中提取n-gram,并将n-gram映射及其计数存储在映射中。现在,Map键是字符串,其中可以包含1,2,3个单词。
例如(“ mango”,10),(“ facbook friend”,6),(“ the rich guy”,3)1 <= n <= 3
映射示例:
(“ mango”,2)
(“苹果”,1)
(“芒果树”,5)
(“ facebook朋友”,3)
(“ facebook人物”,8)
(“ Bougth新手表”,2)
现在,我想根据地图关键字中的关键字标记长度对地图进行排序。就像所有1个单词的键映射都应该首先在map中,然后是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,芒果树= 5,购买了新手表= 2}
答案 0 :(得分:1)
以下代码按顺序插入记录。
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);
答案 1 :(得分:0)
您可以使用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)))