Advertisement
jaVer404

level18.lesson10.home08(done and tested)

Jan 14th, 2016
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.07 KB | None | 0 0
  1. package com.javarush.test.level18.lesson10.home08;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.FileInputStream;
  5. import java.io.IOException;
  6. import java.io.InputStreamReader;
  7. import java.util.ArrayList;
  8. import java.util.HashMap;
  9. import java.util.HashSet;
  10. import java.util.Map;
  11.  
  12. /* Нити и байты
  13. Читайте с консоли имена файлов, пока не будет введено слово "exit"
  14. Передайте имя файла в нить ReadThread
  15. Нить ReadThread должна найти байт, который встречается в файле максимальное число раз, и добавить его в словарь resultMap,
  16. где параметр String - это имя файла, параметр Integer - это искомый байт.
  17. Закрыть потоки. Не использовать try-with-resources
  18. */
  19.  
  20. public class Solution {
  21.     public static Map<String, Integer> resultMap = new HashMap<String, Integer>();
  22.  
  23.     public static void main(String[] args) throws IOException{
  24.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  25.         String consoleInput = "";
  26.         while (true) {
  27.             consoleInput=reader.readLine();
  28.             if (consoleInput.equals("exit")) {
  29.                 break;
  30.             }
  31.             else {
  32.                 new ReadThread(consoleInput).start();//it would be different threads
  33.             }
  34.         }
  35.         reader.close();
  36. /*            for (Map.Entry<String, Integer> entry : resultMap.entrySet()) {
  37.             String key = entry.getKey().toString();;
  38.             Integer value = entry.getValue();
  39.             System.out.println("key: " + key + " value: " + value );
  40.         }*/
  41.  
  42.     }
  43.  
  44.     public static class ReadThread extends Thread {
  45.  
  46.         public ReadThread(String name)
  47.         {  //implement constructor body
  48.             super(name);
  49.         }
  50.         // implement file reading here - реализуйте чтение из файла тут
  51.  
  52.  
  53.         @Override
  54.         public void run()
  55.         {
  56.             /*-----------1. Reading bytes to ArrayList-----------------------*/
  57.             ArrayList <Integer> intBytes = new ArrayList<Integer>();
  58.             FileInputStream myFile = null;
  59.  
  60.             try
  61.             {
  62.                 myFile = new FileInputStream(this.getName());
  63.                 boolean eof = false;
  64.                 while (!eof) {
  65.                     int byteValue = myFile.read();
  66.                     intBytes.add(byteValue);
  67.                     if (byteValue == -1) {
  68.                         eof = true;
  69.                     }
  70.                 }
  71.             }
  72.             catch (IOException e) {
  73.                 //System.out.println("On read exception");
  74.             }
  75.             finally
  76.             {
  77.                 if (myFile != null) {
  78.                     try
  79.                     {
  80.                         myFile.close();
  81.                     }
  82.                     catch (Exception e1) {
  83.                         //System.out.println("On close exception");
  84.                     }
  85.                 }
  86.             }
  87. /*------------------------Ent of file reading--------------------------*/
  88.             //and now we have array list with all Integer (bytes) inside
  89.             //From list we will add them to HashSet (no doubles)
  90.             HashSet<Integer> intByteSet = new HashSet<Integer>();
  91.             for (int i : intBytes) {
  92.                 intByteSet.add(i);
  93.             }
  94.             //Now we have Set of uniqe values
  95. /*----------------------------------------------------------------------*/
  96.             //We take value from intByteSet and count it in the ArrayList
  97.             HashMap<Integer,Integer> mapOfBytes = new HashMap<Integer, Integer>();
  98.             for (int i : intByteSet) {
  99.                 mapOfBytes.put(i,countSpecByte(i,intBytes));
  100.             }
  101. /*----------------------------------------------------------------------*/
  102.  
  103.             //And now we have HashMap with int key and a number of encounts
  104.             //and we need to find most common byte and add it to the main Map
  105. /*---------------------------------------------------------------------------*/
  106.  
  107.             Map.Entry<Integer, Integer> maxEntry = null;
  108.             for (Map.Entry<Integer, Integer> entry : mapOfBytes.entrySet())
  109.             {
  110.                 if (maxEntry == null || entry.getValue().compareTo(maxEntry.getValue()) > 0)//может тут и два одинаковых влепить
  111.                 {
  112.                     maxEntry = entry;
  113.                 }
  114.             }
  115.             resultMap.put(this.getName(), maxEntry.getKey());
  116. /*--------------------------------------------------------------------------------------*/
  117.         }
  118.     }
  119.     public static int countSpecByte (int myByte, ArrayList<Integer> arrayInts)/*Не нужно тут выкидывать исключения*/
  120.     {
  121.         int counter = 0;
  122.         for (int b : arrayInts) {
  123.             if (myByte == b) {
  124.                 counter++;
  125.             }
  126.         }
  127.         return  counter;
  128.     }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement