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);
}
}
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);
}
}
Get Answers For Free
Most questions answered within 1 hours.