Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions java-collections.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/resources" type="java-test-resource" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" scope="TEST" name="Maven: org.junit.jupiter:junit-jupiter:5.5.2" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.junit.jupiter:junit-jupiter-api:5.5.2" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.apiguardian:apiguardian-api:1.1.0" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.opentest4j:opentest4j:1.2.0" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.junit.platform:junit-platform-commons:1.5.2" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.junit.jupiter:junit-jupiter-params:5.5.2" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.junit.jupiter:junit-jupiter-engine:5.5.2" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.junit.platform:junit-platform-engine:1.5.2" level="project" />
</component>
</module>
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
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.*;

/**
* Совет:
Expand All @@ -23,7 +24,12 @@ public class SimpleTextStatisticsAnalyzer implements TextStatisticsAnalyzer {
*/
@Override
public int countSumLengthOfWords(String text) {
return 0;
List<String> words = getWords(text);
int countOfWords = 0;
for (String word : words) {
countOfWords += word.length();
}
return countOfWords;
}

/**
Expand All @@ -34,7 +40,8 @@ public int countSumLengthOfWords(String text) {
*/
@Override
public int countNumberOfWords(String text) {
return 0;
List<String> words = getWords(text);
return words.size();
}

/**
Expand All @@ -44,7 +51,16 @@ public int countNumberOfWords(String text) {
*/
@Override
public int countNumberOfUniqueWords(String text) {
return 0;
List<String> words = getWords(text);
List<String> repeatedWords = new ArrayList<>();
int counterOfUniqueWords = 0;
for (String word : words) {
if (!repeatedWords.contains(word)) {
repeatedWords.add(word);
counterOfUniqueWords++;
}
}
return counterOfUniqueWords;
}

/**
Expand All @@ -57,7 +73,11 @@ public int countNumberOfUniqueWords(String text) {
*/
@Override
public List<String> getWords(String text) {
return emptyList();
Pattern pattern = Pattern.compile("[\\W+]");
Matcher matcher = pattern.matcher(text);
String textIntermediateVersion = matcher.replaceAll("1");

return Arrays.asList(textIntermediateVersion.split("1+"));
}

/**
Expand All @@ -70,7 +90,9 @@ public List<String> getWords(String text) {
*/
@Override
public Set<String> getUniqueWords(String text) {
return emptySet();
List<String> words = getWords(text);
return new HashSet<>(words);

}

/**
Expand All @@ -82,9 +104,29 @@ public Set<String> getUniqueWords(String text) {
*/
@Override
public Map<String, Integer> countNumberOfWordsRepetitions(String text) {
return emptyMap();
List<String> words = getWords(text);
List<String> repeatedWords = new ArrayList<>();
Map<String, Integer> countedWords = new HashMap<>();
for (String word : words) {
if (!repeatedWords.contains(word)) {
countedWords.put(word, countRepeats(word, words));
repeatedWords.add(word);
}
}
return countedWords;

}

private int countRepeats(String word, List<String> list) {
int counter = 0;
for (String wordInList : list) {
counter += wordInList.equals(word) ? 1 : 0;
}
return counter;

}


/**
* Необходимо реализовать функционал вывода слов из текста в отсортированном виде (по длине) в зависимости от параметра direction.
* Например для текста "Hello, Hi, mother, father - good, cat, c!!" должны вернуться результаты :
Expand All @@ -95,6 +137,11 @@ public Map<String, Integer> countNumberOfWordsRepetitions(String text) {
*/
@Override
public List<String> sortWordsByLength(String text, Direction direction) {
return emptyList();
List<String> words = getWords(text);
Comparator<String> comparator = Comparator.comparing(String::length);
comparator = direction == Direction.ASC ? comparator : comparator.reversed();
words.sort(comparator);

return words;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@

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 static java.util.Collections.*;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
* Данный класс обязан использовать StreamApi из функционала Java 8. Функциональность должна быть идентична
Expand All @@ -16,36 +14,62 @@
public class StreamApiTextStatisticsAnalyzer implements TextStatisticsAnalyzer {
@Override
public int countSumLengthOfWords(String text) {
return 0;
List<String> words = getWords(text);
return words.stream().reduce(0, (sum, s) -> sum += s.length(), Integer::sum);
}


@Override
public int countNumberOfWords(String text) {
return 0;
List<String> words = getWords(text);
return words.stream().reduce(0, (sum, s) -> sum += 1, Integer::sum);

}

@Override
public int countNumberOfUniqueWords(String text) {
return 0;
List<String> words = getWords(text);
List<String> repeatedWords = new ArrayList<>();
return (int) words.stream().filter((o1) -> !repeatedWords.contains(o1)).map(repeatedWords::add).count();
}

@Override
public List<String> getWords(String text) {
return emptyList();
Pattern pattern = Pattern.compile("[\\W+]");
Matcher matcher = pattern.matcher(text);

String textVer2 = matcher.replaceAll("1");
String[] wordsArray = textVer2.split("1+");
return Arrays.stream(wordsArray).collect(Collectors.toList());


}

@Override
public Set<String> getUniqueWords(String text) {
return emptySet();
List<String> words = getWords(text);
return words.stream().collect(Collectors.toSet());
}

@Override
public Map<String, Integer> countNumberOfWordsRepetitions(String text) {
return emptyMap();
List<String> words = getWords(text);
Set<String> wordSet = new HashSet<>(words);
Map<String, Integer> countedWords = new HashMap<>();
wordSet.stream().forEach((s) -> countedWords.put(s, countRepeats(s, words)));
return countedWords;
}

private int countRepeats(String word, List<String> list) {
return (int) list.stream().filter(s -> s.equals(word)).count();
}

@Override
public List<String> sortWordsByLength(String text, Direction direction) {
return emptyList();
List<String> words = getWords(text);
Comparator<String> comparator = Comparator.comparing(String::length);
comparator = direction == Direction.ASC ? comparator : comparator.reversed();
return words.stream().sorted(comparator).collect(Collectors.toList());

}
}