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>
5 changes: 1 addition & 4 deletions src/main/java/com/epam/izh/rd/online/Main.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package com.epam.izh.rd.online;

import java.io.IOException;
import java.net.URISyntaxException;

public class Main {

public static void main(String[] args) throws IOException, URISyntaxException {
public static void main(String[] args) {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

import java.util.*;

import static java.util.Collections.*;
import static com.epam.izh.rd.online.helper.Direction.ASC;


/**
* Совет:
Expand All @@ -23,7 +24,8 @@ public class SimpleTextStatisticsAnalyzer implements TextStatisticsAnalyzer {
*/
@Override
public int countSumLengthOfWords(String text) {
return 0;
return text.replaceAll("[\\p{Punct}\\s]+",
"").length();
}

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

/**
Expand All @@ -44,7 +46,7 @@ public int countNumberOfWords(String text) {
*/
@Override
public int countNumberOfUniqueWords(String text) {
return 0;
return new HashSet<>(getWords(text)).size();
}

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

/**
Expand All @@ -70,7 +72,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 +84,12 @@ public Set<String> getUniqueWords(String text) {
*/
@Override
public Map<String, Integer> countNumberOfWordsRepetitions(String text) {
return emptyMap();
Map<String, Integer> amountOfWordsRepetitions = new Hashtable<>();

for (String s : getUniqueWords(text)) {
amountOfWordsRepetitions.put(s, Collections.frequency(getWords(text), s));
}
return amountOfWordsRepetitions;
}

/**
Expand All @@ -95,6 +102,13 @@ public Map<String, Integer> countNumberOfWordsRepetitions(String text) {
*/
@Override
public List<String> sortWordsByLength(String text, Direction direction) {
return emptyList();
List<String> wordsByLength = getWords(text);
if (direction.equals(ASC)) {
wordsByLength.sort(((o1, o2) -> o1.length() - o2.length()));
} else {
wordsByLength.sort(((o1, o2) -> -(o1.length() - o2.length())));
}
return wordsByLength;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +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 static java.util.Collections.*;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;


/**
* Данный класс обязан использовать StreamApi из функционала Java 8. Функциональность должна быть идентична
Expand All @@ -16,36 +15,55 @@
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 getWords(text)
.size();
}

@Override
public int countNumberOfUniqueWords(String text) {
return 0;
return new HashSet<>(getWords(text))
.size();
}

@Override
public List<String> getWords(String text) {
return emptyList();
Stream<String> stream = Stream.of(text.split("\\W+"));
return stream
.collect(Collectors.toList());
}

@Override
public Set<String> getUniqueWords(String text) {
return emptySet();
return new HashSet<>(getWords(text));
}

@Override
public Map<String, Integer> countNumberOfWordsRepetitions(String text) {
return emptyMap();
return getWords(text)
.stream()
.collect(Collectors
.toMap(word -> word, word -> 1, Integer::sum));
}

@Override
public List<String> sortWordsByLength(String text, Direction direction) {
return emptyList();
return getWords(text)
.stream()
.sorted(getStringComparator(direction))
.collect(Collectors.toList());
}

private Comparator<String> getStringComparator(Direction direction) {
return direction.equals(Direction.ASC) ?
Comparator.comparing(String::length) :
Comparator.comparing(String::length).reversed();
}
}