This exercise is designed to help familiarize the student with choosing the righ
ID: 3786810 • Letter: T
Question
This exercise is designed to help familiarize the student with choosing the right data structure for the right problem. If implemented, the parts of this exercise should be done by making use of an implementation of the relevant interface (Stack, Queue, Deque, Map, SortedMap, USet, or SSet) provided by the Java Collections Framework.
Solve the following three problems by reading a text file one line at a time and performing operations on each line in the appropriate data structure(s). Your implementations should be fast enough that even files containing a million lines can be processed in a few seconds. Different solutions are possible your solution will be evaluated based on correctness and efficiency.
You are asked to develop a simple application that provides the following functionalities.
1. Read the input one line at a time and, than provide an output consisting in:
a. a text file named ?CompFile.txt (e.g. File1CompFile.txt) including all the input lines with no duplicates and ordered according to their first occurrences in the input. For example the sequence of lines consisting of karim, bob, lol, bob, alice, bob, lol will be transformed to karim, bob, lol, alice.
b. a text file named ?UncompArg.txt (e.g. File1UncompArg.txt) that stores supplementary information allowing to retrieve the input before processing as required in the following functionality.
2. Process any two files output of the previous functionality (e.g. File1CompFile.txt and File1UnompArg.txt) producing the original input that may contain duplicated lines (e.g. File1.txt).
Explanation / Answer
comfile.txt
karim,bob,lol,bob,alice,bob,lol
program
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
public class HashSetDemo {
public static void main(String[] args) throws IOException {
Set<String> set = new HashSet<String>();// to create set object
FileWriter writer = new FileWriter(new File("D:\UncompArg.txt"));// to
// create
// writer
// object
BufferedReader reader = new BufferedReader(new FileReader(new File(
"D:\comfile.txt")));
String line = "";
while ((line = reader.readLine()) != null)// to read the file
{
writer.write(line, 0, line.length());
writer.flush();// to flush the content in writer object
// System.out.println(line);
String[] words = line.split(",");
for (String word : words) {
set.add(word);
}
}
Iterator itr = set.iterator();// to display the elements in the set
// object
System.out.println(" displaying without duplicates");
while (itr.hasNext()) {
System.out.println(itr.next());
}
// the process of anyone of the file
BufferedReader reader1 = new BufferedReader(new FileReader(new File(
"D:\UncompArg.txt")));
String line1 = "";
System.out.println("the process of original file content");
while ((line1 = reader1.readLine()) != null)// to read the file
{
System.out.println(line1);
}
}
}
output
displaying without duplicates
karim
bob
lol
alice
the process of original file content
karim,bob,lol,bob,alice,bob,lol
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.