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
@@ -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)}.
Expand All @@ -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;
}

/**
Expand All @@ -34,7 +35,7 @@ public int countSumLengthOfWords(String text) {
*/
@Override
public int countNumberOfWords(String text) {
return 0;
return getWords(text).size();
}

/**
Expand All @@ -44,7 +45,7 @@ public int countNumberOfWords(String text) {
*/
@Override
public int countNumberOfUniqueWords(String text) {
return 0;
return getUniqueWords(text).size();
}

/**
Expand All @@ -57,7 +58,7 @@ public int countNumberOfUniqueWords(String text) {
*/
@Override
public List<String> getWords(String text) {
return emptyList();
return Arrays.asList(text.split("[\\W]+"));
}

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

/**
Expand All @@ -82,7 +83,15 @@ public Set<String> getUniqueWords(String text) {
*/
@Override
public Map<String, Integer> countNumberOfWordsRepetitions(String text) {
return emptyMap();
Map<String, Integer> repeatWords = new HashMap<>();
for (String word : getWords(text)) {
Integer q = repeatWords.put(word, 1);
if (q != null) {
repeatWords.put(word, q + 1);
}

}
return repeatWords;
}

/**
Expand All @@ -95,6 +104,13 @@ public Map<String, Integer> countNumberOfWordsRepetitions(String text) {
*/
@Override
public List<String> sortWordsByLength(String text, Direction direction) {
return emptyList();
List<String> words = new ArrayList<>(getUniqueWords(text));
words.sort(Comparator.comparingInt(String::length));
if (direction.name().equals("DESC")) {
Collections.reverse(words);
}
return words;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -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. Функциональность должна быть идентична
Expand All @@ -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<String> getWords(String text) {
return emptyList();
return Stream.of(text.split("[\\W]+")).collect(Collectors.toList());
}

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

@Override
public Map<String, Integer> countNumberOfWordsRepetitions(String text) {
return emptyMap();
return getWords(text).stream().collect(Collectors.groupingBy(s -> s, reducing(0, e -> 1, Integer::sum)));
}

@Override
public List<String> 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());

}
}