在java中将ArrayList转换为Hashset

时间:2018-01-24 11:02:07

标签: java arraylist hashset

我有这个代码可以读取和计算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]”上有错误告诉我它必须解析为字符串。谢谢

2 个答案:

答案 0 :(得分:1)

正如评论中所说,您不能使用[]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");
} 

答案 1 :(得分:0)

我使用地图数据结构来存储和更新字词及其各自的频率。

根据您的要求:每个单词都应计入一次,即使它们在一行中多次出现。

迭代每一行:

 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.. 
        }
    }
}