Question

Solve Project 2 using a TreeMap. You can display the words in key sequence without performing...

Solve Project 2 using a TreeMap. You can display the words in key sequence without performing a sort. Java

Project 2: Use a HashMap to store the frequency counts for all the words in a large text document. When you are done, display the contents of this HashMap. Next, create a set view of the Map and store its contents in an array. Then sort the array based on key value and display it. Finally, sort the array in decreasing order by frequency and display it.

Syntax for Project 2:

import java.util.*;
import java.io.*;


public class WordFrequencies {

    @SuppressWarnings({"unchecked"})
    public static void main(String[] args) throws Exception {
        String filename = "test.txt";
        Map<String,Integer> frequencies = new HashMap<String,Integer>();
        Scanner sc = new Scanner(new File(filename));
        while (sc.hasNext()) {
            String word = sc.next();
            if (frequencies.containsKey(word)) {
                // increment the frequency count for this word by 1
                frequencies.put(word,frequencies.get(word)+1);
            } else {
                // start the frequency count at 1
                frequencies.put(word,1);
            }
        }
               
        System.out.println(frequencies);
       
        Set<Map.Entry<String,Integer>> entries = frequencies.entrySet();
       
        // Store the contents of entries in an array of Map.Entry<String,Integer>
        // This will cause an unchecked conversion warning unless we suppress it
        Map.Entry<String,Integer> [] entryArray = entries.toArray(new Map.Entry[0]);
               
        Arrays.sort(entryArray, new Comparator<Map.Entry<String,Integer>>() {
            public int compare(Map.Entry<String,Integer> o1, Map.Entry<String,Integer> o2) {
                return o1.getKey().compareTo(o2.getKey());
            }
        });
       
        for (Map.Entry<String,Integer> entry : entryArray) System.out.println(entry);
       
        Arrays.sort(entryArray, new Comparator<Map.Entry<String,Integer>>() {
            public int compare(Map.Entry<String,Integer> o1, Map.Entry<String,Integer> o2) {
                return o1.getValue().compareTo(o2.getValue());
            }
        });
        for (Map.Entry<String,Integer> entry : entryArray) System.out.println(entry);
       
    }

}

Homework Answers

Answer #1

import java.util.*;
import java.io.*;


public class WordFrequencies {

    @SuppressWarnings({"unchecked"})
    public static void main(String[] args) throws Exception {
        String filename = "test.txt";
        Map<String,Integer> frequencies = new HashMap<String,Integer>();
        Scanner sc = new Scanner(new File(filename));
        while (sc.hasNext()) {
            String word = sc.next();
            if (frequencies.containsKey(word)) {
                // increment the frequency count for this word by 1
                frequencies.put(word,frequencies.get(word)+1);
            } else {
                // start the frequency count at 1
                frequencies.put(word,1);
            }
        }
               
        System.out.println(frequencies);
       
        Set<Map.Entry<String,Integer>> entries = frequencies.entrySet();
       
        // Store the contents of entries in an array of Map.Entry<String,Integer>
        // This will cause an unchecked conversion warning unless we suppress it
        Map.Entry<String,Integer> [] entryArray = entries.toArray(new Map.Entry[0]);
               
        Arrays.sort(entryArray, new Comparator<Map.Entry<String,Integer>>() {
            public int compare(Map.Entry<String,Integer> o1, Map.Entry<String,Integer> o2) {
                return o1.getKey().compareTo(o2.getKey());
            }
        });
       
        for (Map.Entry<String,Integer> entry : entryArray) System.out.println(entry);
       
        Arrays.sort(entryArray, new Comparator<Map.Entry<String,Integer>>() {
            public int compare(Map.Entry<String,Integer> o1, Map.Entry<String,Integer> o2) {
                return o1.getValue().compareTo(o2.getValue());
            }
        });
        for (Map.Entry<String,Integer> entry : entryArray) System.out.println(entry);
       
    }

}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
10.6 LAB: Implement a stack using an array Given main() complete the Stack class by writing...
10.6 LAB: Implement a stack using an array Given main() complete the Stack class by writing the methods push() and pop(). The stack uses an array of size 5 to store elements. The command Push followed by a positive number pushes the number onto the stack. The command Pop pops the top element from the stack. Entering -1 exits the program. Ex. If the input is Push 1 Push 2 Push 3 Push 4 Push 5 Pop -1 the output...
Using JAVA For this assignment, you will analyze code that uses a file input stream and...
Using JAVA For this assignment, you will analyze code that uses a file input stream and a file output stream. Read through the linked Java™ code. In a Microsoft® Word document, answer the following questions: Could this program be run as is? If not, what is it lacking? Does this program modify the contents of an input stream? In what way? What are the results of running this code? ********************************************** CODE TO ANALYZE  ******************************************************** /********************************************************************** *   Program:   Datasort *   Purpose:   ...
Question 3 a) Add a new class named CAS (Contract Academic Staff) to the project. It...
Question 3 a) Add a new class named CAS (Contract Academic Staff) to the project. It must inherit from Professor, but add a new String attribute named term. (The term must be a six digit string, with the year followed by two digits to represent the term: "01" for Winter, "05" for Spring, and "09" for Fall. Thus, 201705 represents the Spring 2017 term. Override print so that it prints CAS data like: McGarrity, Ivan Department: English Term: 201705 Write...
In this problem, you will write an implementation of BubbleSort. Your function should take in a...
In this problem, you will write an implementation of BubbleSort. Your function should take in a single line representing an array of integers, and output a single line containing the list in ascending order. For example, if you receive the following input followed by a newline: 8 7 6 5 4 3 2 1 then you should display the following output followed by a newline: 1 2 3 4 5 6 7 8 Starter code for reading the input and...
Task 1: You will modify the add method in the LinkedBag class.Add a second parameter to...
Task 1: You will modify the add method in the LinkedBag class.Add a second parameter to the method header that will be a boolean variable: public boolean add(T newEntry, boolean sorted) The modification to the add method will makeit possible toadd new entriesto the beginning of the list, as it does now, but also to add new entries in sorted order. The sorted parameter if set to false will result in the existing functionality being executed (it will add the...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT