From 62c58114d85f567e7a057a378f629472c4ca11c8 Mon Sep 17 00:00:00 2001 From: Mikhail Gorshkov Date: Mon, 19 Oct 2020 17:42:47 +0400 Subject: [PATCH 01/10] STEP1 --- java-collections.iml | 24 +++++++++++++++++++ .../service/SimpleTextStatisticsAnalyzer.java | 22 +++++++++++++---- 2 files changed, 41 insertions(+), 5 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..d346a66 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 @@ -3,6 +3,8 @@ import com.epam.izh.rd.online.helper.Direction; import java.util.*; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import static java.util.Collections.*; @@ -23,7 +25,8 @@ public class SimpleTextStatisticsAnalyzer implements TextStatisticsAnalyzer { */ @Override public int countSumLengthOfWords(String text) { - return 0; + int count = text.replaceAll("\\W+", "").length(); + return count; } /** @@ -34,7 +37,8 @@ public int countSumLengthOfWords(String text) { */ @Override public int countNumberOfWords(String text) { - return 0; + int count = getWords(text).size(); + return count; } /** @@ -44,7 +48,8 @@ public int countNumberOfWords(String text) { */ @Override public int countNumberOfUniqueWords(String text) { - return 0; + int count = getUniqueWords(text).size(); + return count; } /** @@ -57,7 +62,12 @@ public int countNumberOfUniqueWords(String text) { */ @Override public List getWords(String text) { - return emptyList(); + List list = new ArrayList<>(); + String[] splStr = text.replaceAll("\\W+", " ").split(" "); + for (String elem : splStr){ + list.add(elem); + } + return list; } /** @@ -70,7 +80,9 @@ public List getWords(String text) { */ @Override public Set getUniqueWords(String text) { - return emptySet(); + Set uniq = new HashSet<>(getWords(text)) ; + + return uniq; } /** From 992df8e123bc57963b7a36850776e88f5ec1971b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CMikhail?= Date: Mon, 19 Oct 2020 22:55:38 +0400 Subject: [PATCH 02/10] =?UTF-8?q?=D0=B4=D0=BE=D0=B4=D0=B5=D0=BB=D0=B0?= =?UTF-8?q?=D1=82=D1=8C=20sortWordsByLength?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- java-collections.iml | 24 ------------------- .../service/SimpleTextStatisticsAnalyzer.java | 17 +++++++++++-- .../StreamApiTextStatisticsAnalyzer.java | 1 + 3 files changed, 16 insertions(+), 26 deletions(-) delete mode 100644 java-collections.iml diff --git a/java-collections.iml b/java-collections.iml deleted file mode 100644 index dd43669..0000000 --- a/java-collections.iml +++ /dev/null @@ -1,24 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - \ 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 d346a66..831260f 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 @@ -2,6 +2,7 @@ import com.epam.izh.rd.online.helper.Direction; +import java.lang.reflect.Array; import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -94,9 +95,16 @@ public Set getUniqueWords(String text) { */ @Override public Map countNumberOfWordsRepetitions(String text) { - return emptyMap(); + Map keyValue = new HashMap(); + for (String elem : getWords(text)){ + Integer k = Collections.frequency(getWords(text),elem); + keyValue.put(elem,k); + } + return keyValue; } + + /** * Необходимо реализовать функционал вывода слов из текста в отсортированном виде (по длине) в зависимости от параметра direction. * Например для текста "Hello, Hi, mother, father - good, cat, c!!" должны вернуться результаты : @@ -107,6 +115,11 @@ public Map countNumberOfWordsRepetitions(String text) { */ @Override public List sortWordsByLength(String text, Direction direction) { - return emptyList(); + + List sortWord = new ArrayList<>(); + sortWord.addAll(getWords(text)); + Collections.sort(sortWord); + + return sortWord; } } 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..3903c00 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 @@ -31,6 +31,7 @@ public int countNumberOfUniqueWords(String text) { @Override public List getWords(String text) { + return emptyList(); } From 5d992d1110acadead9f147d7c0be6d8ec19c9fb7 Mon Sep 17 00:00:00 2001 From: Mikhail Gorshkov Date: Tue, 20 Oct 2020 17:25:29 +0400 Subject: [PATCH 03/10] sort --- .../service/SimpleTextStatisticsAnalyzer.java | 11 +++++++---- .../service/StreamApiTextStatisticsAnalyzer.java | 15 +++++++++++---- 2 files changed, 18 insertions(+), 8 deletions(-) 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 831260f..5446a9f 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 @@ -115,11 +115,14 @@ public Map countNumberOfWordsRepetitions(String text) { */ @Override public List sortWordsByLength(String text, Direction direction) { + ArrayList rez = new ArrayList(); + rez.addAll(getWords(text)); + for(int i = 0; i < rez.size() - 1; i++) { + String cur = (String) rez.get(i); + String next = (String) rez.get(i+1); - List sortWord = new ArrayList<>(); - sortWord.addAll(getWords(text)); - Collections.sort(sortWord); + } - return sortWord; + return emptyList(); } } 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 3903c00..2830323 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,7 @@ 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 static java.util.Collections.*; @@ -47,6 +44,16 @@ public Map countNumberOfWordsRepetitions(String text) { @Override public List sortWordsByLength(String text, Direction direction) { + List rez = new ArrayList<>(); + rez.addAll(getWords(text)); + if (direction.equals(Direction.DESC)){ + rez.sort(Comparator.comparing(String::length)); + return rez; + }else if (direction.equals(Direction.ASC)){ + rez.sort(Comparator.comparing(String::length)); + Collections.reverse(rez); + return rez; + } return emptyList(); } } From d6f0190092c7b552e54b95f218fd78bd845fdeeb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CMikhail?= Date: Wed, 21 Oct 2020 00:22:22 +0400 Subject: [PATCH 04/10] bloc 1 done --- .../service/SimpleTextStatisticsAnalyzer.java | 28 ++++++++++++++----- 1 file changed, 21 insertions(+), 7 deletions(-) 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 831260f..f6afa52 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 @@ -7,7 +7,7 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; -import static java.util.Collections.*; +import static java.util.Collections.emptyList; /** * Совет: @@ -115,11 +115,25 @@ public Map countNumberOfWordsRepetitions(String text) { */ @Override public List sortWordsByLength(String text, Direction direction) { - - List sortWord = new ArrayList<>(); - sortWord.addAll(getWords(text)); - Collections.sort(sortWord); - - return sortWord; + List rez2Word = new ArrayList<>(); + String [] strArr = new String[getWords(text).size()]; + for (int i = 0; i < getWords(text).size(); i++) { + strArr[i] = getWords(text).get(i); + } + for(int i=0;istrArr[j].length()){ + String temp= strArr[i]; + strArr[i]=strArr[j]; + strArr[j]=temp; + } + } + } + rez2Word.addAll(Arrays.asList(strArr)); + if (direction.equals(Direction.DESC)){ + Collections.reverse(rez2Word); + return rez2Word; + } + return rez2Word; } } From 4c4e32b4b5ae97475b8ed0dafdc25472763f163d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CMikhail?= Date: Wed, 21 Oct 2020 22:44:46 +0400 Subject: [PATCH 05/10] bloc 2 begin --- .../StreamApiTextStatisticsAnalyzer.java | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) 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 3903c00..6a7dc17 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.Collector; +import java.util.stream.Collectors; import static java.util.Collections.*; @@ -31,17 +30,22 @@ public int countNumberOfUniqueWords(String text) { @Override public List getWords(String text) { - - return emptyList(); + String[] splStr = text.replaceAll("\\W+", " ").split(" "); + List words = Arrays.stream(splStr).collect(Collectors.toList()); + return words; } @Override public Set getUniqueWords(String text) { - return emptySet(); + Set uniq = getWords(text).stream().collect(Collectors.toSet()); + return uniq; } @Override public Map countNumberOfWordsRepetitions(String text) { + Map keyValue = getWords(text).stream() + .collect(Collectors.toMap(elem -> Collections.frequency(getWords(text),elem)), Collectors.joining(":"),); + return emptyMap(); } From 72971964664d70bf5c79354d775b81dbd77ee213 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CMikhail?= Date: Wed, 21 Oct 2020 23:12:20 +0400 Subject: [PATCH 06/10] bloc 2 begin --- .../online/service/StreamApiTextStatisticsAnalyzer.java | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) 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 6a7dc17..d79d650 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 @@ -43,14 +43,17 @@ public Set getUniqueWords(String text) { @Override public Map countNumberOfWordsRepetitions(String text) { - Map keyValue = getWords(text).stream() - .collect(Collectors.toMap(elem -> Collections.frequency(getWords(text),elem)), Collectors.joining(":"),); +// Map keyValue = getWords(text).stream() +// .collect(Collectors.toMap(elem -> Collections.frequency(getWords(text),elem)), Collectors.joining(":"),); return emptyMap(); } @Override public List sortWordsByLength(String text, Direction direction) { - return emptyList(); + List wordSort= new ArrayList<>(getWords(text)); + wordSort.sort(Comparator.comparing(String::length)); + + return wordSort; } } From b42c925499b2d670b18ad53af1f893c1eddb3ca8 Mon Sep 17 00:00:00 2001 From: Mikhail Gorshkov Date: Thu, 22 Oct 2020 17:14:10 +0400 Subject: [PATCH 07/10] sort length --- .../service/StreamApiTextStatisticsAnalyzer.java | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) 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 d79d650..4d0be5a 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 @@ -44,16 +44,22 @@ public Set getUniqueWords(String text) { @Override public Map countNumberOfWordsRepetitions(String text) { // Map keyValue = getWords(text).stream() -// .collect(Collectors.toMap(elem -> Collections.frequency(getWords(text),elem)), Collectors.joining(":"),); +// .collect(Collectors.toMap(Collections.frequency(), Collectors.joining(":"),); return emptyMap(); } @Override public List sortWordsByLength(String text, Direction direction) { - List wordSort= new ArrayList<>(getWords(text)); - wordSort.sort(Comparator.comparing(String::length)); + ArrayList wordSort = new ArrayList<>(); + + wordSort.addAll(getWords(text).stream().sorted(Comparator.comparing(String::length)).collect(Collectors.toList())); + if (direction.equals(Direction.ASC)) { + return wordSort; + } else { + reverse(wordSort); + return wordSort; + } - return wordSort; } } From dff5f3ced3d9248b51bdb4ff03f786eb83458184 Mon Sep 17 00:00:00 2001 From: Mikhail Gorshkov Date: Thu, 22 Oct 2020 17:47:56 +0400 Subject: [PATCH 08/10] sort count --- .../rd/online/service/StreamApiTextStatisticsAnalyzer.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) 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 4d0be5a..f766dc8 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 @@ -5,6 +5,7 @@ import java.util.*; import java.util.stream.Collector; import java.util.stream.Collectors; +import java.util.stream.IntStream; import static java.util.Collections.*; @@ -15,12 +16,14 @@ public class StreamApiTextStatisticsAnalyzer implements TextStatisticsAnalyzer { @Override public int countSumLengthOfWords(String text) { + return 0; } @Override public int countNumberOfWords(String text) { - return 0; + long count = getWords(text).stream().count(); + return (int) count; } @Override @@ -60,6 +63,5 @@ public List sortWordsByLength(String text, Direction direction) { reverse(wordSort); return wordSort; } - } } From 071dfcd31cc702d1202f9230f0f6c2f92fe1c1d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CMikhail?= Date: Fri, 23 Oct 2020 01:30:38 +0400 Subject: [PATCH 09/10] bloc 2 --- .../StreamApiTextStatisticsAnalyzer.java | 22 +++++++++++++------ 1 file changed, 15 insertions(+), 7 deletions(-) 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 f766dc8..1938be2 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 @@ -3,11 +3,13 @@ import com.epam.izh.rd.online.helper.Direction; import java.util.*; +import java.util.function.Function; import java.util.stream.Collector; import java.util.stream.Collectors; import java.util.stream.IntStream; import static java.util.Collections.*; +import static java.util.stream.Collectors.counting; /** * Данный класс обязан использовать StreamApi из функционала Java 8. Функциональность должна быть идентична @@ -16,8 +18,8 @@ public class StreamApiTextStatisticsAnalyzer implements TextStatisticsAnalyzer { @Override public int countSumLengthOfWords(String text) { - - return 0; + int count =getWords(text).stream().collect(Collectors.summingInt(s->s.length())); + return count; } @Override @@ -28,7 +30,8 @@ public int countNumberOfWords(String text) { @Override public int countNumberOfUniqueWords(String text) { - return 0; + int count = (int) getWords(text).stream().distinct().count(); + return count; } @Override @@ -46,16 +49,21 @@ public Set getUniqueWords(String text) { @Override public Map countNumberOfWordsRepetitions(String text) { -// Map keyValue = getWords(text).stream() -// .collect(Collectors.toMap(Collections.frequency(), Collectors.joining(":"),); - return emptyMap(); + Map keyValue = getWords(text).stream() + .collect(Collectors.groupingBy(String::valueOf,Collectors.counting())); + keyValue.values().stream().collect(Collectors.toMap(Function.identity(), i -> (int) i.in)); + + + + + + return cc; } @Override public List sortWordsByLength(String text, Direction direction) { ArrayList wordSort = new ArrayList<>(); - wordSort.addAll(getWords(text).stream().sorted(Comparator.comparing(String::length)).collect(Collectors.toList())); if (direction.equals(Direction.ASC)) { return wordSort; From 07c9fdf1604479a8903525666cc7e6943bba2bec Mon Sep 17 00:00:00 2001 From: Mikhail Gorshkov Date: Tue, 27 Oct 2020 10:16:57 +0400 Subject: [PATCH 10/10] done --- .../service/StreamApiTextStatisticsAnalyzer.java | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) 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 1938be2..036bf5d 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 @@ -9,6 +9,7 @@ import java.util.stream.IntStream; import static java.util.Collections.*; +import static java.util.stream.Collectors.averagingInt; import static java.util.stream.Collectors.counting; /** @@ -49,16 +50,7 @@ public Set getUniqueWords(String text) { @Override public Map countNumberOfWordsRepetitions(String text) { - - Map keyValue = getWords(text).stream() - .collect(Collectors.groupingBy(String::valueOf,Collectors.counting())); - keyValue.values().stream().collect(Collectors.toMap(Function.identity(), i -> (int) i.in)); - - - - - - return cc; + return getWords(text).stream().collect(Collectors.toMap(s -> s,s -> 1,Integer::sum)); } @Override