Feature required in #73 (#77)

This commit is contained in:
César Aguilera
2017-05-09 18:16:11 +05:30
committed by Shekhar Gulati
parent 605eb4f2a5
commit 84b3303718
2 changed files with 63 additions and 3 deletions
+24 -1
View File
@@ -34,6 +34,9 @@ import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Stream;
import static java.util.function.Function.identity;
import static java.util.stream.Collectors.counting;
import static java.util.stream.Collectors.groupingBy;
import static java.util.stream.Collectors.joining;
/**
@@ -87,7 +90,7 @@ public abstract class Strman {
* @return an Optional String if found else empty
*/
public static Optional<String> at(final String value, int index) {
if (value == null || value.isEmpty()) {
if (isNullOrEmpty(value)) {
return Optional.empty();
}
int length = value.length();
@@ -97,6 +100,10 @@ public abstract class Strman {
return (index < length && index >= 0) ? Optional.of(String.valueOf(value.charAt(index))) : Optional.empty();
}
private static boolean isNullOrEmpty(String input) {
return input == null || input.isEmpty();
}
/**
* Returns an array with strings between start and end.
*
@@ -1302,4 +1309,20 @@ public abstract class Strman {
}
return countSubstr(value.substring(offset), subStr, allowOverlapping, ++count);
}
/**
* Counts the number of occurrences of each character in the string
*
* @param input The input string
* @return A map containing the number of occurrences of each character in the string
*/
public static Map<Character, Long> charsCount(String input) {
if (isNullOrEmpty(input)) {
return Collections.emptyMap();
}
return input.chars()
.mapToObj(c -> (char) c)
.collect(groupingBy(identity(), counting()));
}
}