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 @@ -23,7 +23,11 @@ public class SimpleTextStatisticsAnalyzer implements TextStatisticsAnalyzer {
*/
@Override
public int countSumLengthOfWords(String text) {
return 0;
int cum = 0 ;
for (String word: getWords(text)) {
cum += word.length();
}
return cum;
}

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

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

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

/**
Expand All @@ -70,7 +75,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 +87,17 @@ public Set<String> getUniqueWords(String text) {
*/
@Override
public Map<String, Integer> countNumberOfWordsRepetitions(String text) {
return emptyMap();
Map<String, Integer> map = new HashMap<>();
for (String word: getWords(text)) {
if(map.containsKey(word)){
int count = map.get(word) + 1 ;
map.put(word, count);
}else {
map.put(word,1);
}
}
System.out.println(map);
return map;
}

/**
Expand All @@ -95,6 +110,16 @@ public Map<String, Integer> countNumberOfWordsRepetitions(String text) {
*/
@Override
public List<String> sortWordsByLength(String text, Direction direction) {
return emptyList();
Comparator<String> comparator = Comparator.comparing(String::length);
List<String> sortList = getWords(text);
if(direction.equals(Direction.DESC)){
sortList.sort(comparator.reversed());
System.out.println(sortList);
}
if(direction.equals(Direction.ASC)){
sortList.sort(comparator);
System.out.println(sortList);
}
return sortList;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.Collectors;
import java.util.stream.Stream;

import static java.util.Collections.*;

Expand All @@ -16,36 +15,49 @@
public class StreamApiTextStatisticsAnalyzer implements TextStatisticsAnalyzer {
@Override
public int countSumLengthOfWords(String text) {
return 0;
return Stream.of(text.split("[^\\w]+"))
.mapToInt((w)->w.length())
.sum();
}

@Override
public int countNumberOfWords(String text) {
return 0;
return Stream.of(text.split("[^\\w]+"))
.mapToInt((w)->1)
.sum();
}

@Override
public int countNumberOfUniqueWords(String text) {
return 0;
return (int) Stream.of(text.split("[^\\w]+"))
.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 Stream.of(text.split("[^\\w]+"))
.collect(Collectors.toSet());
}

@Override
public Map<String, Integer> countNumberOfWordsRepetitions(String text) {
return emptyMap();
return Stream.of(text.split("[^\\w]+"))
.collect(Collectors.groupingBy(w->w,Collectors.summingInt(w->1)));
}

@Override
public List<String> sortWordsByLength(String text, Direction direction) {
return emptyList();
return Stream.of(text.split("[^\\w]+"))
.sorted(direction.equals(Direction.ASC)?
Comparator.comparing(String::length)
: Comparator.comparing(String::length).reversed())
.collect(Collectors.toList());
}
}