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..3d3a07d 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 @@ -23,7 +23,11 @@ public class SimpleTextStatisticsAnalyzer implements TextStatisticsAnalyzer { */ @Override public int countSumLengthOfWords(String text) { - return 0; + int cum = 0 ; + for (String word: getWords(text)) { + cum += word.length(); + } + return cum; } /** @@ -34,7 +38,7 @@ public int countSumLengthOfWords(String text) { */ @Override public int countNumberOfWords(String text) { - return 0; + return getWords(text).size(); } /** @@ -44,7 +48,7 @@ public int countNumberOfWords(String text) { */ @Override public int countNumberOfUniqueWords(String text) { - return 0; + return new HashSet<>(getWords(text)).size(); } /** @@ -57,7 +61,8 @@ public int countNumberOfUniqueWords(String text) { */ @Override public List getWords(String text) { - return emptyList(); + List textList = Arrays.asList(text.split("[^\\w]+")); + return textList; } /** @@ -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,17 @@ public Set getUniqueWords(String text) { */ @Override public Map countNumberOfWordsRepetitions(String text) { - return emptyMap(); + Map map = new HashMap<>(); + for (String word: getWords(text)) { + if(map.containsKey(word)){ + int count = map.get(word) + 1 ; + map.put(word, count); + }else { + map.put(word,1); + } + } + System.out.println(map); + return map; } /** @@ -95,6 +110,16 @@ public Map countNumberOfWordsRepetitions(String text) { */ @Override public List sortWordsByLength(String text, Direction direction) { - return emptyList(); + Comparator comparator = Comparator.comparing(String::length); + List sortList = getWords(text); + if(direction.equals(Direction.DESC)){ + sortList.sort(comparator.reversed()); + System.out.println(sortList); + } + if(direction.equals(Direction.ASC)){ + sortList.sort(comparator); + System.out.println(sortList); + } + return sortList; } } 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..fdfefae 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,10 +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 java.util.stream.Stream; import static java.util.Collections.*; @@ -16,36 +15,49 @@ public class StreamApiTextStatisticsAnalyzer implements TextStatisticsAnalyzer { @Override public int countSumLengthOfWords(String text) { - return 0; + return Stream.of(text.split("[^\\w]+")) + .mapToInt((w)->w.length()) + .sum(); } @Override public int countNumberOfWords(String text) { - return 0; + return Stream.of(text.split("[^\\w]+")) + .mapToInt((w)->1) + .sum(); } @Override public int countNumberOfUniqueWords(String text) { - return 0; + return (int) Stream.of(text.split("[^\\w]+")) + .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 Stream.of(text.split("[^\\w]+")) + .collect(Collectors.toSet()); } @Override public Map countNumberOfWordsRepetitions(String text) { - return emptyMap(); + return Stream.of(text.split("[^\\w]+")) + .collect(Collectors.groupingBy(w->w,Collectors.summingInt(w->1))); } @Override public List sortWordsByLength(String text, Direction direction) { - return emptyList(); + return Stream.of(text.split("[^\\w]+")) + .sorted(direction.equals(Direction.ASC)? + Comparator.comparing(String::length) + : Comparator.comparing(String::length).reversed()) + .collect(Collectors.toList()); } }