简体   繁体   中英

How to verify if an object with a particular attribute value exists in an arraylist?

I would like to read through aa text document and then add only the unique words to the arraylist of "Word" objects. It appears that the code I have now does not enter any words at all into the wordList arraylist.

public ArrayList<Word> wordList = new ArrayList<Word>();
    String fileName, word;
    int counter;
    Scanner reader = null;
    Scanner scanner = new Scanner(System.in);

try {
            reader = new Scanner(new FileInputStream(fileName));
        }
        catch(FileNotFoundException e) {
            System.out.println("The file could not be found. The program will now exit.");
            System.exit(0);
        }

    while (reader.hasNext()) {
                word = reader.next().toLowerCase();
                for (Word value : wordList) {
                    if(value.getValue().contains(word)) {
                        Word newWord = new Word(word);
                        wordList.add(newWord);
                    }
                }
                counter++;
            }

    public class Word {

        String value;
        int frequency;

        public Word(String v) {
            value = v;
            frequency = 1;
        }

        public String getValue() {
            return value;
        }

    public String toString() {
        return value + " " + frequency;
    }

}

Alright, let's start by fixing your current code. The issue you have is that you are only adding a new word object to the list when one already exists. Instead, you need to add a new Word object when none exist, and increment the frequency otherwise. Here is an example fix for that:

    ArrayList<Word> wordList = new ArrayList<Word>();
    String fileName, word;
    Scanner reader = null;
    Scanner scanner = new Scanner(System.in);

    try {
        reader = new Scanner(new FileInputStream(fileName));
    }
    catch(FileNotFoundException e) {
        System.out.println("The file could not be found. The program will now exit.");
        System.exit(0);
    }

    while (reader.hasNext()) {
            word = reader.next().toLowerCase();
            boolean wordExists = false;
            for (Word value : wordList) {
               // We have seen the word before so increase frequency.
               if(value.getValue().equals(word)) {
                    value.frequency++;
                    wordExists = true;
                    break;
               }
            }
            // This is the first time we have seen the word!
            if (!wordExists) {
                Word newValue = new Word(word);
                newValue.frequency = 1;
                wordList.add(newValue);
             }
        }
}

However, this is a really bad solution (O(n^2) runtime). Instead we should be using datastructure known as a Map which will bring our runtime down to (O(n))

    ArrayList<Word> wordList = new ArrayList<Word>();
    String fileName, word;
    int counter;
    Scanner reader = null;
    Scanner scanner = new Scanner(System.in);

    try {
        reader = new Scanner(new FileInputStream(fileName));
    }
    catch(FileNotFoundException e) {
        System.out.println("The file could not be found. The program will now exit.");
        System.exit(0);
    }
    Map<String, Integer> frequencyMap = new HashMap<String, Integer>();
    while (reader.hasNext()) {
       word = reader.next().toLowerCase();
       // This is equivalent to searching every word in the list via hashing (O(1))
       if(!frequencyMap.containsKey(word)) {
          frequencyMap.put(word, 1);
       } else {
          // We have already seen the word, increase frequency.
          frequencyMap.put(word, frequencyMap.get(word) + 1);
       } 
    }

    // Convert our map of word->frequency to a list of Word objects.
    for(Map.Entry<String, Integer> entry : frequencyMap.entrySet()) {
      Word word = new Word(entry.getKey());
      word.frequency = entry.getValue();
      wordList.add(word);
    }
}

Your for-each loop is iterating over wordList , but that is an empty ArrayList so your code will never reach the wordList.add(newWord); line

I appreciate that perhaps you wanted critique on why your algorithm wasn't working, or maybe it was an example of a much larger problem but if all you want to do is count occurences, there is a much simpler way of doing this.

Using Streams in Java 8 you can boil this down to one method - create a Stream of the lines in the file, lowercase them and then use a Collector to count them.

public static void main(final String args[]) throws IOException
{
    final File file = new File(System.getProperty("user.home") + File.separator + "Desktop" + File.separator + "myFile.txt");

    for (final Entry<String, Long> entry : countWordsInFile(file).entrySet())
    {
        System.out.println(entry);
    }
}

public static Map<String, Long> countWordsInFile(final File file) throws IOException
{
    return Files.lines(file.toPath()).map(String::toLowerCase).collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
}

I've not done anything with Streams until now so any critique welcome.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM