[英]Converting ArrayList to Hashset in java
我有這段代碼可以讀取並計算txt文件中的每個單詞,但是我只希望它對一行中的每個單詞進行一次計數,因此我試圖創建一個HashSet,但是我無法將ArrayList轉換為HashSet的。 這是我的代碼:
try {
List<String> list = new ArrayList<String>();
int totalWords = 0;
int uniqueWords = 0;
File fr = new File("filename.txt");
Scanner sc = new Scanner(fr);
while (sc.hasNext()) {
String words = sc.next();
String[] space = words.split(" ");
Set<String> set = new HashSet<String>(Arrays.asList(space));
for (int i = 0; i < set.length; i++) {
list.add(set[i]);
}
totalWords++;
}
System.out.println("Words with their frequency..");
Set<String> uniqueSet = new HashSet<String>(list);
for (String word : uniqueSet) {
System.out.println(word + ": " + Collections.frequency(list,word));
}
} catch (Exception e) {
System.out.println("File not found");
}
如果有人可以幫助您解釋為什么長度“無法解析或不是字段”,以及為什么我對“ set [i]”有錯誤,告訴我必須將其解析為字符串。 謝謝
正如在注釋中告訴您的那樣,您不能使用[]
或length
因為任何Set
都是Collection
而不是數組:
您可以這樣嘗試:
try {
List<String> list = new ArrayList<String>();
int totalWords = 0;
int uniqueWords = 0;
File fr = new File("filename.txt");
Scanner sc = new Scanner(fr);
while (sc.hasNext()) {
String words = sc.next();
String[] space = words.split(" ");
Set<String> set = new HashSet<String>(Arrays.asList(space));
for(String element : set){
list.add(element);
}
totalWords++;
}
System.out.println("Words with their frequency..");
Set<String> uniqueSet = new HashSet<String>(list);
for (String word : uniqueSet) {
System.out.println(word + ": " + Collections.frequency(list,word));
}
} catch (Exception e) {
System.out.println("File not found");
}
我已經使用了地圖數據結構來存儲和更新單詞及其各自的頻率。
根據您的要求:即使每個單詞在同一行中多次出現,每個單詞也應僅計數一次 。
遍歷每一行:
Store all the words in the set.
Now just iterate over this set and update the map data structure.
因此,最后對應於映射中的單詞的值將是所需的頻率。
您可以在下面查看我的代碼:
import java.io.File;
import java.util.*;
public class sol {
public static void main(String args[]) {
try {
File fr = new File("file.txt");
Scanner sc = new Scanner(fr);
// to maintain frequency of each word after reading each line..
HashMap<String, Integer> word_frequency = new HashMap<String, Integer>();
while(sc.hasNextLine()) {
// input the line..
String line = sc.nextLine();
String words[] = line.split(" ");
// just store which unique words are there in this line..
HashSet<String> unique_words = new HashSet<String>();
for(int i=0;i<words.length;i++) {
unique_words.add(words[i]); // add it to set..
}
// Iterate on the set now to update the frequency..
Iterator itr = unique_words.iterator();
while(itr.hasNext()) {
String word = (String)itr.next();
// If this word is already there then just increment it..
if(word_frequency.containsKey(word)) {
int old_frequency = word_frequency.get(word);
int new_frequency = old_frequency + 1;
word_frequency.put(word, new_frequency);
}
else {
// If this word is not there then put this
// new word in the map with frequency 1..
word_frequency.put(word, 1);
}
}
}
// Now, you have all the words with their respective frequencies..
// Just print the words and their frequencies..
for(Map.Entry obj : word_frequency.entrySet()) {
String word = (String)obj.getKey();
int frequency = (Integer)obj.getValue();
System.out.println(word+": "+frequency);
}
}
catch(Exception e) {
// throw whatever exception you wish..
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.