From 80d4a866b50196fd053c2450f3e8a073e560a0af Mon Sep 17 00:00:00 2001 From: Vlad Date: Thu, 1 Oct 2020 00:17:41 +0400 Subject: [PATCH] Added realisation of the list and stream api --- java-collections.iml | 24 +++++++++++++ .../service/SimpleTextStatisticsAnalyzer.java | 36 +++++++++++++------ .../StreamApiTextStatisticsAnalyzer.java | 27 +++++++++----- 3 files changed, 68 insertions(+), 19 deletions(-) create mode 100644 java-collections.iml 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..18c4d2a 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 @@ -1,11 +1,8 @@ package com.epam.izh.rd.online.service; import com.epam.izh.rd.online.helper.Direction; - import java.util.*; -import static java.util.Collections.*; - /** * Совет: * Начните с реализации метода {@link SimpleTextStatisticsAnalyzer#getWords(String)}. @@ -23,7 +20,11 @@ public class SimpleTextStatisticsAnalyzer implements TextStatisticsAnalyzer { */ @Override public int countSumLengthOfWords(String text) { - return 0; + int countSumLengthOfWords = 0; + for (String word : getWords(text)) { + countSumLengthOfWords += word.length(); + } + return countSumLengthOfWords; } /** @@ -34,7 +35,7 @@ public int countSumLengthOfWords(String text) { */ @Override public int countNumberOfWords(String text) { - return 0; + return getWords(text).size(); } /** @@ -44,7 +45,7 @@ public int countNumberOfWords(String text) { */ @Override public int countNumberOfUniqueWords(String text) { - return 0; + return getUniqueWords(text).size(); } /** @@ -57,7 +58,7 @@ public int countNumberOfUniqueWords(String text) { */ @Override public List getWords(String text) { - return emptyList(); + return Arrays.asList(text.split("[\\W]+")); } /** @@ -70,7 +71,7 @@ public List getWords(String text) { */ @Override public Set getUniqueWords(String text) { - return emptySet(); + return new HashSet<>(getWords(text)); } /** @@ -82,7 +83,15 @@ public Set getUniqueWords(String text) { */ @Override public Map countNumberOfWordsRepetitions(String text) { - return emptyMap(); + Map repeatWords = new HashMap<>(); + for (String word : getWords(text)) { + Integer q = repeatWords.put(word, 1); + if (q != null) { + repeatWords.put(word, q + 1); + } + + } + return repeatWords; } /** @@ -95,6 +104,13 @@ public Map countNumberOfWordsRepetitions(String text) { */ @Override public List sortWordsByLength(String text, Direction direction) { - return emptyList(); + List words = new ArrayList<>(getUniqueWords(text)); + words.sort(Comparator.comparingInt(String::length)); + if (direction.name().equals("DESC")) { + Collections.reverse(words); + } + 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..7a33f15 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,14 @@ import com.epam.izh.rd.online.helper.Direction; -import java.util.Collections; +import java.util.Comparator; import java.util.List; import java.util.Map; import java.util.Set; +import java.util.stream.Collectors; +import java.util.stream.Stream; -import static java.util.Collections.*; +import static java.util.stream.Collectors.reducing; /** * Данный класс обязан использовать StreamApi из функционала Java 8. Функциональность должна быть идентична @@ -16,36 +18,43 @@ 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 Math.toIntExact(getWords(text).stream().count()); } @Override public int countNumberOfUniqueWords(String text) { - return 0; + return Math.toIntExact(getWords(text).stream().distinct().count()); } @Override public List getWords(String text) { - return emptyList(); + return Stream.of(text.split("[\\W]+")).collect(Collectors.toList()); } @Override public Set getUniqueWords(String text) { - return emptySet(); + return getWords(text).stream().collect(Collectors.toSet()); } @Override public Map countNumberOfWordsRepetitions(String text) { - return emptyMap(); + return getWords(text).stream().collect(Collectors.groupingBy(s -> s, reducing(0, e -> 1, Integer::sum))); } @Override public List sortWordsByLength(String text, Direction direction) { - return emptyList(); + + return getWords(text).stream() + .sorted( + direction == Direction.ASC ? + Comparator.comparing(String::length) : + Comparator.comparing(String::length).reversed() + ).collect(Collectors.toList()); + } }