Advertisement
Guest User

Untitled

a guest
Mar 24th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.90 KB | None | 0 0
  1. package com.javarush.task.task18.task1823;
  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.HashMap;
  8. import java.util.Iterator;
  9. import java.util.Map;
  10.  
  11. /*
  12. Нити и байты
  13. */
  14.  
  15. public class Solution
  16. {
  17. public static Map<String, Integer> resultMap = new HashMap<String, Integer> ( );
  18.  
  19. public static void main(String[] args) throws IOException
  20. {
  21. BufferedReader reader = new BufferedReader (new InputStreamReader (System.in));
  22. String name;
  23. while (!(name = reader.readLine ( )).contains ("exit"))
  24. {
  25. new ReadThread (name).start ( );
  26.  
  27. }
  28. // System.out.println (resultMap);
  29. reader.close ( );
  30. }
  31.  
  32. public static class ReadThread extends Thread
  33. {
  34. String fileName;
  35.  
  36. public ReadThread(String fileName)
  37. {
  38. //implement constructor body
  39. this.fileName = fileName;
  40. }
  41.  
  42. @Override
  43. public void run()
  44. {
  45. super.run ( );
  46. try
  47. {
  48. int res = readMax (fileName);
  49. resultMap.put (fileName, res);
  50. }
  51. catch (IOException e)
  52. {
  53. e.printStackTrace ( );
  54. }
  55. }
  56. // implement file reading here - реализуйте чтение из файла тут
  57. public int readMax(String fileName) throws IOException
  58. {
  59. FileInputStream inputStream = new FileInputStream (fileName);
  60. HashMap<Integer, Integer> map = new HashMap<> ( );
  61. int max = 0;
  62. while (inputStream.available ( ) > 0) //пока остались непрочитанные байты
  63. {
  64. int data = Integer.valueOf (inputStream.read ( )); //прочитать очередной байт
  65. if (map.containsKey (data))
  66. {
  67. int value = map.get (data);
  68. map.replace (data, value + 1);
  69. if ((value + 1) > max)
  70. max = value + 1;
  71. }
  72. else
  73. {
  74. map.put (data, 1);
  75. }
  76. }
  77. inputStream.close ( ); // закрываем поток
  78. Iterator it = map.entrySet ( ).iterator ( );
  79. while (it.hasNext ( ))
  80. {
  81. HashMap.Entry pair = (Map.Entry) it.next ( );
  82. if (pair.getValue ( ).equals (max))
  83. {
  84. return (int) pair.getKey ( );
  85. }
  86. }
  87. return max;
  88. }
  89. }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement