diff --git a/java-collections.iml b/java-collections.iml new file mode 100644 index 0000000..dd43669 --- /dev/null +++ b/java-collections.iml @@ -0,0 +1,24 @@ + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/java/com/epam/izh/rd/online/service/SimpleTextStatisticsAnalyzer.java b/src/main/java/com/epam/izh/rd/online/service/SimpleTextStatisticsAnalyzer.java index 32f8e35..a79343c 100644 --- a/src/main/java/com/epam/izh/rd/online/service/SimpleTextStatisticsAnalyzer.java +++ b/src/main/java/com/epam/izh/rd/online/service/SimpleTextStatisticsAnalyzer.java @@ -4,8 +4,6 @@ import java.util.*; -import static java.util.Collections.*; - /** * Совет: * Начните с реализации метода {@link SimpleTextStatisticsAnalyzer#getWords(String)}. @@ -21,9 +19,16 @@ public class SimpleTextStatisticsAnalyzer implements TextStatisticsAnalyzer { * * @param text текст */ + @Override public int countSumLengthOfWords(String text) { - return 0; + ArrayList words = (ArrayList) getWords(text); + int lengthOfWords = 0; + for (String word : words) { + lengthOfWords = word.length() + lengthOfWords; + } + return lengthOfWords; + } /** @@ -34,7 +39,7 @@ public int countSumLengthOfWords(String text) { */ @Override public int countNumberOfWords(String text) { - return 0; + return getWords(text).size(); } /** @@ -44,7 +49,7 @@ public int countNumberOfWords(String text) { */ @Override public int countNumberOfUniqueWords(String text) { - return 0; + return getUniqueWords(text).size(); } /** @@ -57,7 +62,7 @@ public int countNumberOfUniqueWords(String text) { */ @Override public List getWords(String text) { - return emptyList(); + return new ArrayList<>(Arrays.asList(text.split("\\W+"))); } /** @@ -70,7 +75,7 @@ public List getWords(String text) { */ @Override public Set getUniqueWords(String text) { - return emptySet(); + return new HashSet<>(getWords(text)); } /** @@ -82,7 +87,13 @@ public Set getUniqueWords(String text) { */ @Override public Map countNumberOfWordsRepetitions(String text) { - return emptyMap(); + List words = getWords(text); + Map countNumberOfWordsRepetitionsMap = new LinkedHashMap<>(); + for (String word : words) { + int repetitionCount = Collections.frequency(words, word); + countNumberOfWordsRepetitionsMap.put(word, repetitionCount); + } + return countNumberOfWordsRepetitionsMap; } /** @@ -95,6 +106,16 @@ public Map countNumberOfWordsRepetitions(String text) { */ @Override public List sortWordsByLength(String text, Direction direction) { - return emptyList(); + ArrayList words = (ArrayList) getWords(text); + words.sort(new Comparator() { + @Override + public int compare(String o1, String o2) { + return direction == Direction.ASC ? o1.length() - o2.length() : o2.length() - o1.length(); + } + }); + + return words; } + + } diff --git a/src/main/java/com/epam/izh/rd/online/service/StreamApiTextStatisticsAnalyzer.java b/src/main/java/com/epam/izh/rd/online/service/StreamApiTextStatisticsAnalyzer.java index e9b8957..0fa11f4 100644 --- a/src/main/java/com/epam/izh/rd/online/service/StreamApiTextStatisticsAnalyzer.java +++ b/src/main/java/com/epam/izh/rd/online/service/StreamApiTextStatisticsAnalyzer.java @@ -2,12 +2,9 @@ import com.epam.izh.rd.online.helper.Direction; -import java.util.Collections; -import java.util.List; -import java.util.Map; -import java.util.Set; +import java.util.*; +import java.util.stream.Collectors; -import static java.util.Collections.*; /** * Данный класс обязан использовать StreamApi из функционала Java 8. Функциональность должна быть идентична @@ -16,36 +13,46 @@ public class StreamApiTextStatisticsAnalyzer implements TextStatisticsAnalyzer { @Override public int countSumLengthOfWords(String text) { - return 0; + return getWords(text).stream().mapToInt(String::length).sum(); } @Override public int countNumberOfWords(String text) { - return 0; + return (int) getWords(text).stream().count(); } @Override public int countNumberOfUniqueWords(String text) { - return 0; + return (int) getWords(text).stream().distinct().count(); + } @Override public List getWords(String text) { - return emptyList(); + ArrayList words = new ArrayList<>(); + String[] wordsArray = text.split("\\W+"); + Arrays.stream(wordsArray).forEach(words::add); + return words; + } @Override public Set getUniqueWords(String text) { - return emptySet(); + return getWords(text).stream().collect(Collectors.toSet()); + } @Override public Map countNumberOfWordsRepetitions(String text) { - return emptyMap(); + return getUniqueWords(text).stream().collect(Collectors.toMap(e -> e, + e -> Collections.frequency(getWords(text), e))); + } @Override public List sortWordsByLength(String text, Direction direction) { - return emptyList(); + return getWords(text).stream().sorted((o1, o2) -> direction == Direction.ASC ? o1.length() - o2.length() : o2.length() - o1.length()) + .collect(Collectors.toList()); + } }