Guest User

Untitled

a guest
Nov 10th, 2016
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. import static java.util.stream.Collectors.counting;
  2. import static java.util.stream.Collectors.groupingBy;
  3.  
  4. import java.io.*;
  5. import java.nio.*;
  6. import java.util.*;
  7. import java.nio.channels.FileChannel;
  8. import java.util.function.Supplier;
  9. import java.util.stream.Stream;
  10.  
  11. public class Main {
  12. public static void main(String... args) {
  13. try (RandomAccessFile f = new RandomAccessFile(new File("/tmp/wtf"), "r")) {
  14. long fileSize = f.length();
  15. MappedByteBuffer mem = f.getChannel().map(FileChannel.MapMode.READ_ONLY, 0, fileSize);
  16. Map<Integer, Long> freqStat = Stream.generate(unsignedByteSupplier(mem))
  17. .limit(fileSize)
  18. .collect(groupingBy(identity(), counting()));
  19. Long topFreq = topFrequency(freqStat);
  20. showKeysByValue(freqStat, topFreq);
  21. } catch (IOException e) {
  22. e.printStackTrace();
  23. }
  24. }
  25.  
  26. static Supplier<Integer> unsignedByteSupplier(ByteBuffer buff) {
  27. return () -> Byte.toUnsignedInt(buff.get());
  28. }
  29.  
  30. static Long topFrequency(Map<Integer, Long> stat) {
  31. return stat.values().stream().max(Long::compare).get();
  32. }
  33.  
  34. static void showKeysByValue(Map<Integer, Long> map, Long value) {
  35. map.entrySet().stream()
  36. .filter(e -> Objects.equals(e.getValue(), value))
  37. .map(Map.Entry::getKey)
  38. .forEach(System.out::println);
  39. }
  40. }
Add Comment
Please, Sign In to add comment