Question

JAVA! Algorithm using Java Collections Framework. Please only answer if you can help... Basically we need...

JAVA! Algorithm using Java Collections Framework. Please only answer if you can help...

Basically we need to find an algorithm using an EFFICIENT JCF in order to get to the solution... for this problem, I need to go through a text file and output only the last 8888 lines in order that they appear...if there are fewer lines, then print all of those... But my code is too slow because I store the all lines (s.add(lines)) i think...and I can't find a way to make it faster ! How can I make this work correctly and fast?

public static void doIt(BufferedReader r, PrintWriter w) throws IOException {
   ArrayList s = new ArrayList<>();
   ArrayList t = new ArrayList<>();


   for (String line = r.readLine(); line != null; line = r.readLine()) {
      s.add(line);
   }

   if (s.size() <= 8888) {
      for (int i = 0; i < s.size(); i++) {
         t.add(s.get(i));
      }
   } else if (s.size() > 8888) {
      for (int i = s.size() - 8888; i < s.size(); i++) {
         t.add(s.get(i));
      }
   }
   System.out.println(t);
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

public static void doIt(BufferedReader r, PrintWriter w) throws IOException {
       ArrayList s = new ArrayList<String>();
       ArrayList t = new ArrayList<String>();
       Queue q = new PriorityQueue<String>();

       for (String line = r.readLine(); line != null; line = r.readLine()) {
           s.add(line);
           if (q.size() < 8888)
               q.add(line);
           else {
               q.remove();
               q.add(line);
           }
       }
       t.addAll(q);

      
   }

// if the the count increases than q will remove the first inserted lines and keeps the latest lines so it only keeps the 8888

so finally we are adding the all values from q to t

Add a comment
Know the answer?
Add Answer to:
JAVA! Algorithm using Java Collections Framework. Please only answer if you can help... Basically we need...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Java only. Thanks These questions involve choosing the right abstraction (Collection, Set, List, Queue, Deque, SortedSet,...

    Java only. Thanks These questions involve choosing the right abstraction (Collection, Set, List, Queue, Deque, SortedSet, Map, or SortedMap) to EFFICIENTLY accomplish the task at hand. The best way to do these is to read the question and then think about what type of Collection is best to use to solve it. There are only a few lines of code you need to write to solve each of them. Unless specified otherwise, sorted order refers to the natural sorted order...

  • Please answer question correctly and show the result of the working code. The actual and demo...

    Please answer question correctly and show the result of the working code. The actual and demo code is provided .must take command line arguments of text files for ex : input.txt output.txt from a filetext(any kind of input for the text file should work as it is for testing the question) This assignment is about using the Java Collections Framework to accomplish some basic text-processing tasks. These questions involve choosing the right abstraction (Collection, Set, List, Queue, Deque, SortedSet, Map,...

  • Please answer the question correctly and USE THE MOST EFFICIENT TECHNIQUE OF JAVA COLLECTION FRAME WORK...

    Please answer the question correctly and USE THE MOST EFFICIENT TECHNIQUE OF JAVA COLLECTION FRAME WORK to answer it. It is very essential that you do. Please make sure it is very efficient and doesnt run out of memory. A demo code required for the question is given below. These questions involve choosing the right abstraction (Collection, Set, List, Queue, Deque, SortedSet, Map, or SortedMap) to efficiently accomplish the task at hand. The best way to do these is to...

  • Please complete the give code (where it says "insert code here") in Java, efficiently. Read the...

    Please complete the give code (where it says "insert code here") in Java, efficiently. Read the entire input one line at a time and then output a subsequence of the lines that appear in the same order they do in the file and that are also in non-decreasing or non-increasing sorted order. If the file contains n lines, then the length of this subsequence should be at least sqrt(n). For example, if the input contains 9 lines with the numbers...

  • Using java socket programming rewrite the following program to handle multiple clients simultaneously (multi threaded programming)...

    Using java socket programming rewrite the following program to handle multiple clients simultaneously (multi threaded programming) import java.io.*; import java.net.*; public class WelcomeClient { public static void main(String[] args) throws IOException {    if (args.length != 2) { System.err.println( "Usage: java EchoClient <host name> <port number>"); System.exit(1); } String hostName = args[0]; int portNumber = Integer.parseInt(args[1]); try ( Socket kkSocket = new Socket(hostName, portNumber); PrintWriter out = new PrintWriter(kkSocket.getOutputStream(), true); BufferedReader in = new BufferedReader( new InputStreamReader(kkSocket.getInputStream())); ) { BufferedReader...

  • I need help with my IM (instant messaging) java program, I created the Server, Client, and...

    I need help with my IM (instant messaging) java program, I created the Server, Client, and Message class. I somehow can't get both the server and client to message to each other. I am at a roadblock. Here is the question below. Create an IM (instant messaging) java program so that client and server communicate via serialized objects (e.g. of type Message). Each Message object encapsulates the name of the sender and the response typed by the sender. You may...

  • (I don't need code for this question I only need explanation how to solve the question.)...

    (I don't need code for this question I only need explanation how to solve the question.) The following code causes an exception error: import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; public class Ex02 {     public static void main(String[] args) throws IOException {         BufferedReader userInput = new BufferedReader             (new InputStreamReader(System.in));         ArrayList<String> myArr = new ArrayList<String>();         myArr.add("Zero");         myArr.add("One");         myArr.add("Two");         myArr.add("Three");                 System.out.println(myArr.get(7));                     } } Starting with this provided code,...

  • can some one help me solve this and explain it please Input Format A line indicating...

    can some one help me solve this and explain it please Input Format A line indicating the size of the array the array on the next line Constraints n < 10000 Output Format One line printing the array in order input (given to you) One line printing the array, followed by its maximum value Sample Input 0 5 1 3 5 7 9 Sample Output 0 1 3 5 7 9 9 7 5 3 1 9 Contest ends in...

  • I have a below codes with all details and need to answer this below question ONLY!...

    I have a below codes with all details and need to answer this below question ONLY! How to explain below codes with the two properties of a Greedy algorithm - Optimal Substructure and the Greedy Property line by line? ====================== public class TeamFormation { private static ArrayList buildTeam(ArrayList team, int skill) { ArrayList newTeam = new ArrayList(); newTeam.add(skill); for (int player : team) { if (skill == (player + 1)) { newTeam.add(player); } } return newTeam; } private static boolean...

  • composed the following java code to read a string from a text file but receiving compiling...

    composed the following java code to read a string from a text file but receiving compiling errors. The text file is MyNumData.txt. Included the original java script that generated the output file. Shown also in the required output results after running the java program. I can't seem to search for the string and output the results. Any assistance will be greatly appreciated. import java.io.BufferedReader; import java.io.FileReader; import java.util.ArrayList; public class Main {   public static void main(String[] args) {     System.out.print("Enter the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT