Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2018
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.40 KB | None | 0 0
  1. package ru.sbt.collections.Task2;
  2.  
  3. import org.apache.commons.io.IOUtils;
  4. import ru.sbt.collections.Task1.DifferentWords;
  5.  
  6. import java.io.IOException;
  7. import java.io.InputStream;
  8. import java.net.URISyntaxException;
  9. import java.util.*;
  10.  
  11. /**
  12.  * Задание 2: Выведите на экран список различных слов файла, отсортированный по возрастанию их длины.
  13.  */
  14. public class SortedWords {
  15.     public static void main(String[] args) throws IOException, URISyntaxException {
  16.         InputStream resourceAsStream = DifferentWords.class.getResourceAsStream("/ru/sbt/collections/VeryBigText.txt");
  17.         String text = IOUtils.toString(resourceAsStream, "UTF8");
  18.         String[] words = text.split("[ -_—,.;:?!\n\r\t\'\"]");
  19.         TreeSet<String> set = new TreeSet<>((s1, s2) -> {
  20.             int difference = s1.length() - s2.length();
  21.             if (difference == 0) {
  22.                 if (s1.equals(s2))
  23.                     return 0;
  24.                 return 1;
  25.             }
  26.             return difference;
  27.         });
  28.         for (String word : words) {
  29.             if (set.contains(word))
  30.                 continue;
  31.             set.add(word);
  32.         }
  33.  
  34.         for (String word : set) {
  35.             System.out.println(word);
  36.         }
  37.         System.out.println("Всего различных слов: " + set.size());
  38.     }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement