Reputation: 1
String fileName="words.txt"; //words.txt file contains 25,000 words
String word;
try {
FileReader fileReader=new FileReader(fileName);
BufferedReader bufferReader;
ArrayList<String> arrBag;
int count;
bufferReader=new BufferedReader(fileReader);
for (int i=1;i<=maxWordLength;i++) //maxWordLength is 22
{
arrBag = new ArrayList<String> (); // arrBag contains all words with same length and then store to hash map.
count=0;
bufferReader.mark(0);
while((word=bufferReader.readLine())!=null)
{
if (word.length()==i)
{
arrBag.add(word);
count++;
}
}
System.out.println("HashMap key : "+i+" has bag count : "+count);
mapBagOfTasks.put(Integer.toString(i), arrBag); //mapBagOfTasks is HashMap where key is length of word and value is ArrayList of words with same length.
bufferReader.reset();
}
if (fileReader!=null)
{
fileReader.close();
}
}
catch (FileNotFoundException e) {
System.out.println("Input file not found");
e.printStackTrace();
}
catch (IOException e) {
System.out.println("Error while reading File '"+fileName+"'");
e.printStackTrace();
}
I have a "words.txt" file that contains 25,000 words. I want to store all words with same length into an ArrayList and then store it into Hash map as key:length of word & value is array List.
The problem which i faced is that my programe read file first time but does not read same file again. I tried using mark() and reset() funtions but again face same problem. You can see output for justifications. How can i fix this problem?
My programe output is:
max word length in file: 22
HashMap key : 1 has bag count : 26 //(means there are 26 words found of lenth 1)
HashMap key : 2 has bag count : 0
HashMap key : 3 has bag count : 0
HashMap key : 4 has bag count : 0
HashMap key : 5 has bag count : 0
HashMap key : 6 has bag count : 0
HashMap key : 7 has bag count : 0
HashMap key : 8 has bag count : 0
HashMap key : 9 has bag count : 0
HashMap key : 10 has bag count : 0
HashMap key : 11 has bag count : 0
HashMap key : 12 has bag count : 0
HashMap key : 13 has bag count : 0
HashMap key : 14 has bag count : 0
HashMap key : 15 has bag count : 0
HashMap key : 16 has bag count : 0
HashMap key : 17 has bag count : 0
HashMap key : 18 has bag count : 0
HashMap key : 19 has bag count : 0
HashMap key : 20 has bag count : 0
HashMap key : 21 has bag count : 0
HashMap key : 22 has bag count : 0
Upvotes: 0
Views: 360
Reputation: 469
IO is usually the slowest part of any program. Unless you're dealing with files larger than the amount of ram you have available you should read the whole file once into ram and then operate on it there. Based on your description of what you're trying to do this is the code that I would write.
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
public class WordLength {
public static void main(String[] args) {
String fileName = "words.txt";
int maxWordLength = 0;
HashMap<Integer, ArrayList<String>> mapBagOfTasks = new HashMap<>();
// populate the HashMap
try {
BufferedReader br = new BufferedReader(new FileReader(fileName));
String word = "";
while ((word=br.readLine())!=null) {
int count = word.length();
if (count>maxWordLength) {
maxWordLength = count;
}
// if an array list for words of length count is not in the map. put in a new one
if (!mapBagOfTasks.containsKey(count)) {
mapBagOfTasks.put(count, new ArrayList<>());
}
// get the array list for words of length count
ArrayList<String> arrBag = mapBagOfTasks.get(count);
// add word to that array list
arrBag.add(word);
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}
// loop over all of the keys and their values
for (int key=0; key<maxWordLength; key++) {
if (mapBagOfTasks.containsKey(key)) {
ArrayList<String> value = mapBagOfTasks.get(key);
System.out.println("HashMap key : "+key+" has bag count "+value.size());
} else {
System.out.println("HashMap key : "+key+" has bag count 0");
}
}
}
}
Upvotes: 0
Reputation: 1561
Reading from disk is an expensive operation relative to working with data in memory, so you should read the file only once. I suggest you do something like this:
Map<Integer, List<String>> lengthToWords = new HashMap<>();
while ((word = bufferReader.readLine()) != null) {
int length = word.length();
if (length < maxWordLength) {
if (!lengthToWords.containsKey( length ))
lengthToWords.put( length, new ArrayList<>() );
lengthToWords.get( length ).add( word );
}
}
Upvotes: 3