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/ForEx.java b/src/main/java/com/epam/izh/rd/online/service/ForEx.java new file mode 100644 index 0000000..393a8dd --- /dev/null +++ b/src/main/java/com/epam/izh/rd/online/service/ForEx.java @@ -0,0 +1,19 @@ +package com.epam.izh.rd.online.service; + +import java.util.ArrayList; +import java.util.List; + +public class ForEx { + public static void main(String[] args) { + String text ="первое слово, дороже второго"; + List list = new ArrayList<>(); + String[] subStr; + String delimeter = "\\."; // Разделитель + subStr = text.split(delimeter); + // Вывод результата на экран + for(int i = 0; i < subStr.length; i++) { + list.add(subStr[i]); + } + System.out.println(list); + } +} 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..4157e8a 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,7 +2,11 @@ import com.epam.izh.rd.online.helper.Direction; +import java.io.FileInputStream; +import java.io.InputStream; import java.util.*; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import static java.util.Collections.*; @@ -23,7 +27,11 @@ public class SimpleTextStatisticsAnalyzer implements TextStatisticsAnalyzer { */ @Override public int countSumLengthOfWords(String text) { - return 0; + List list = getWords(text); + int count = 0; + for(int i = 0; i list = getWords(text); + int count = list.size(); + return count; } /** @@ -44,7 +54,11 @@ public int countNumberOfWords(String text) { */ @Override public int countNumberOfUniqueWords(String text) { - return 0; + List list = getWords(text); + Set set = new HashSet<>(); + for(int i = 0; i getWords(String text) { - return emptyList(); + Pattern pattern1 = Pattern.compile("[^A-Z a-z\n]"); + Matcher matcher = pattern1.matcher(text); + String str = matcher.replaceAll(""); + List list = new ArrayList<>(); + String strList[]; + strList=str.split("[ \n]+"); + for (int i=0; i getWords(String text) { */ @Override public Set getUniqueWords(String text) { - return emptySet(); + List list = getWords(text); + Set set = new HashSet<>(); + for(int i = 0; i getUniqueWords(String text) { */ @Override public Map countNumberOfWordsRepetitions(String text) { - return emptyMap(); + List list = getWords(text); + Map map = new HashMap<>(); + for(int i = 0; i countNumberOfWordsRepetitions(String text) { */ @Override public List sortWordsByLength(String text, Direction direction) { - return emptyList(); + List list = getWords(text); + Comparator comp = Comparator.comparing(String::length); + + if (direction==Direction.ASC) { + list.sort(comp); + } + else if (direction==Direction.DESC) { + list.sort(comp.reversed()); + } + + return list; } } 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..7ecff72 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,11 @@ 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.lang.reflect.Array; +import java.util.*; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import java.util.stream.Collectors; import static java.util.Collections.*; @@ -16,36 +17,63 @@ public class StreamApiTextStatisticsAnalyzer implements TextStatisticsAnalyzer { @Override public int countSumLengthOfWords(String text) { - return 0; + List list = getWords(text); + return list.stream().reduce(0, (sum, s) -> sum += s.length(), Integer::sum); } @Override public int countNumberOfWords(String text) { - return 0; + List list = getWords(text); + return list.stream().reduce(0, (sum, s) -> sum += 1, Integer::sum); } @Override public int countNumberOfUniqueWords(String text) { - return 0; + List list = getWords(text); + List listApi = list.stream().distinct().collect(Collectors.toList()); + return listApi.size(); } @Override public List getWords(String text) { - return emptyList(); + Pattern pattern1 = Pattern.compile("[^A-Z a-z\n]"); + Matcher matcher = pattern1.matcher(text); + String str = matcher.replaceAll(""); + String strList[]; + strList=str.split("[ \n]+"); + List list = Arrays.stream(strList).collect(Collectors.toList()); + return list; } @Override public Set getUniqueWords(String text) { - return emptySet(); + Set set = getWords(text).stream().collect(Collectors.toSet()); + return set; } @Override public Map countNumberOfWordsRepetitions(String text) { - return emptyMap(); + List list = getWords(text); + Set set = list.stream().collect(Collectors.toSet()); + Map map = new HashMap<>(); + set.stream().forEach((s) -> map.put(s, counter(s, list))); + return map; + } + + private int counter(String word, List list) { + return (int) list.stream().filter(s -> s.equals(word)).count(); } @Override public List sortWordsByLength(String text, Direction direction) { - return emptyList(); + Comparator comp = Comparator.comparing(String::length); + + if (direction==Direction.ASC) { + return getWords(text).stream().sorted(comp).collect(Collectors.toList()); + } + else if (direction==Direction.DESC) { + return getWords(text).stream().sorted(comp.reversed()).collect(Collectors.toList()); + } + return null; } }