Map.Entry<Integer, Integer> entry : freq.entrySet()什么意思
时间: 2023-04-07 20:03:00 浏览: 70
这段代码是在遍历一个Map中的键值对,其中freq是一个Map对象,Map.Entry<Integer, Integer>表示一个键值对,其中Integer表示键的类型,Integer表示值的类型,entry是一个变量名,用于存储遍历到的键值对。
相关问题
Java基于余弦方法计算两个字符串相似度算法工具类
以下是一个Java工具类,基于余弦相似度方法计算两个字符串的相似度:
```java
import java.util.HashMap;
import java.util.Map;
public class StringSimilarityUtil {
/**
* 计算两个字符串的余弦相似度
*
* @param str1 字符串1
* @param str2 字符串2
* @return 余弦相似度
*/
public static double cosineSimilarity(String str1, String str2) {
// 分词
String[] words1 = str1.split(" ");
String[] words2 = str2.split(" ");
// 统计词频
Map<String, Integer> freq1 = new HashMap<>();
Map<String, Integer> freq2 = new HashMap<>();
for (String word : words1) {
freq1.merge(word, 1, Integer::sum);
}
for (String word : words2) {
freq2.merge(word, 1, Integer::sum);
}
// 计算向量长度
double length1 = 0;
double length2 = 0;
for (Map.Entry<String, Integer> entry : freq1.entrySet()) {
length1 += Math.pow(entry.getValue(), 2);
}
for (Map.Entry<String, Integer> entry : freq2.entrySet()) {
length2 += Math.pow(entry.getValue(), 2);
}
length1 = Math.sqrt(length1);
length2 = Math.sqrt(length2);
// 计算内积
double dotProduct = 0;
for (Map.Entry<String, Integer> entry : freq1.entrySet()) {
if (freq2.containsKey(entry.getKey())) {
dotProduct += entry.getValue() * freq2.get(entry.getKey());
}
}
// 计算余弦相似度
return dotProduct / (length1 * length2);
}
}
```
使用方法:
```java
public static void main(String[] args) {
String str1 = "Java是一种计算机编程语言";
String str2 = "Java是一种面向对象的编程语言";
double similarity = StringSimilarityUtil.cosineSimilarity(str1, str2);
System.out.println(similarity); // 输出:0.6666666666666667
}
```
其中,余弦相似度的计算公式为:
$$
similarity = \frac{\sum_{i=1}^{n} A_i \times B_i}{\sqrt{\sum_{i=1}^{n} A_i^2} \times \sqrt{\sum_{i=1}^{n} B_i^2}}
$$
其中,$A_i$和$B_i$分别表示两个字符串中第$i$个词的频次,$n$为总词数。
String - count the frequency of characters in a string Given a string S, count the frequency of characters in the string S (the occurrences of a character present in the string) Input: hello world where: First-line represents the input string Output: d 1 e 1 h 1 l 3 o 2 r 1 w 1 Explanation: Print the count of each distinct letter present in string S. Assumptions: String contains only lower case letters Length of string S can be 0 to 10000
Here's a possible solution to count the frequency of characters in a string:
```
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
Map<Character, Integer> freq = new HashMap<>();
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
freq.put(c, freq.getOrDefault(c, 0) + 1);
}
for (Map.Entry<Character, Integer> entry : freq.entrySet()) {
System.out.println(entry.getKey() + " " + entry.getValue());
}
}
}
```
The code reads a string from the standard input, and then iterates over the characters of the string, updating a map that stores the frequency of each character. The `getOrDefault` method is used to initialize a count of zero for each character that is encountered for the first time. Finally, the code prints the count of each distinct letter present in the string.
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)