Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

java-collections-template done #27

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

import com.epam.izh.rd.online.helper.Direction;

import java.lang.reflect.Array;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static java.util.Collections.*;
import static java.util.Collections.emptyList;

/**
* Совет:
Expand All @@ -23,7 +26,8 @@ public class SimpleTextStatisticsAnalyzer implements TextStatisticsAnalyzer {
*/
@Override
public int countSumLengthOfWords(String text) {
return 0;
int count = text.replaceAll("\\W+", "").length();
return count;
}

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

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

/**
Expand All @@ -57,7 +63,12 @@ public int countNumberOfUniqueWords(String text) {
*/
@Override
public List<String> getWords(String text) {
return emptyList();
List<String> list = new ArrayList<>();
String[] splStr = text.replaceAll("\\W+", " ").split(" ");
for (String elem : splStr){
list.add(elem);
}
return list;
}

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

return uniq;
}

/**
Expand All @@ -82,9 +95,16 @@ public Set<String> getUniqueWords(String text) {
*/
@Override
public Map<String, Integer> countNumberOfWordsRepetitions(String text) {
return emptyMap();
Map keyValue = new HashMap();
for (String elem : getWords(text)){
Integer k = Collections.frequency(getWords(text),elem);
keyValue.put(elem,k);
}
return keyValue;
}



/**
* Необходимо реализовать функционал вывода слов из текста в отсортированном виде (по длине) в зависимости от параметра direction.
* Например для текста "Hello, Hi, mother, father - good, cat, c!!" должны вернуться результаты :
Expand All @@ -95,6 +115,25 @@ public Map<String, Integer> countNumberOfWordsRepetitions(String text) {
*/
@Override
public List<String> sortWordsByLength(String text, Direction direction) {
return emptyList();
List<String> rez2Word = new ArrayList<>();
String [] strArr = new String[getWords(text).size()];
for (int i = 0; i < getWords(text).size(); i++) {
strArr[i] = getWords(text).get(i);
}
for(int i=0;i<strArr.length;i++) {
for(int j=i+1;j<strArr.length;j++){
if(strArr[i].length()>strArr[j].length()){
String temp= strArr[i];
strArr[i]=strArr[j];
strArr[j]=temp;
}
}
}
rez2Word.addAll(Arrays.asList(strArr));
if (direction.equals(Direction.DESC)){
Collections.reverse(rez2Word);
return rez2Word;
}
return rez2Word;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@

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.function.Function;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import static java.util.Collections.*;
import static java.util.stream.Collectors.averagingInt;
import static java.util.stream.Collectors.counting;

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

@Override
public int countNumberOfWords(String text) {
return 0;
long count = getWords(text).stream().count();
return (int) count;
}

@Override
public int countNumberOfUniqueWords(String text) {
return 0;
int count = (int) getWords(text).stream().distinct().count();
return count;
}

@Override
public List<String> getWords(String text) {
return emptyList();
String[] splStr = text.replaceAll("\\W+", " ").split(" ");
List<String> words = Arrays.stream(splStr).collect(Collectors.toList());
return words;
}

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

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

@Override
public List<String> sortWordsByLength(String text, Direction direction) {
return emptyList();
ArrayList<String> wordSort = new ArrayList<>();
wordSort.addAll(getWords(text).stream().sorted(Comparator.comparing(String::length)).collect(Collectors.toList()));
if (direction.equals(Direction.ASC)) {
return wordSort;
} else {
reverse(wordSort);
return wordSort;
}
}
}