Skip to content

Instantly share code, notes, and snippets.

View CustomCollectorCharSummarizer.java
StringUtils.defaultString(arg)
.chars()
.collect(CharSummaryStatistics::new,
CharSummaryStatistics::accept,
CharSummaryStatistics::combine);
View CharSummaryStatistics.java
public class CharSummaryStatistics {
private long totalDigits = 0;
private long totalUppercaseChars = 0;
private long totalLowercaseChars = 0;
private long totalInvalidChars = 0;
public CharSummaryStatistics() {
}
View ImperativeCharSummarizer.java
var totalDigits = 0;
var totalUppercaseChars = 0;
var totalLowercaseChars = 0;
var totalInvalidChars = 0;
for (final char current : string.toCharArray()) {
if (Character.isDigit(current)) {
totalDigits += 1;
} else if (Character.isUpperCase(current)) {
totalUppercaseChars += 1;
} else if (Character.isLowerCase(current)) {
View StreamsCharSummarizer.java
final var totalDigits = string.chars()
.filter(Character::isDigit)
.count();
final var totalUppercaseChars = string.chars()
.filter(Character::isUpperCase)
.count();
final var totalLowercaseChars = string.chars()
.filter(Character::isLowerCase)
View UniqueNumbers.java
final Set<Integer> uniqueNumbers = List.of(1, 2, 2, 3, 4, 5, 5).stream().collect(Collectors.toSet());
View ParameterTypeConverterFactory.java
private Iterable<Object[]> convert(Iterable<Object[]> data) {
List<Object[]> parameters = new ArrayList<>();
for (Object[] args : data) {
Object[] params = new Object[args.length];
for (int i = 0; i < args.length; i++) {
Object param;
Class<?> parameterClazz = method.getMethod()
.getParameterTypes()[i];
if (parameterClazz.equals(String.class)) {
param = buildString(args[i]);
View ParameterConverterFinder.java
private Iterable<Object[]> convert(final Iterable<Object[]> data) {
List<Object[]> parameters = new ArrayList<>();
for (Object[] args : data) {
Object[] params = new Object[args.length];
for (int i = 0; i < args.length; i++) {
final Class<?> parameterClazz = method.getMethod()
.getParameterTypes()[i];
params[i] = convertParameter(args[i], parameterClazz);
}
parameters.add(params);
View ParameterConverterMapping.java
private final Map<Class<?>, ParameterConverter<Object, ?>> convertersByClass = new ImmutableMap.Builder<Class<?>, ParameterConverter<Object, ?>>()
.put(String.class, this::buildString)
.put(Integer.TYPE, this::buildPrimitiveInt)
.put(Integer.class, this::buildInteger)
.put(Long.TYPE, this::buildPrimitiveLong)
.put(Long.class, this::buildLong)
.put(Boolean.TYPE, this::buildPrimitiveBoolean)
.put(Boolean.class, this::buildBoolean)
.put(Short.TYPE, this::buildPrimitiveShort)
.put(Short.class, this::buildShort)
View ParameterConverter.java
@FunctionalInterface
public interface ParameterConverter<T, R> {
R convert(T t);
}
View ListOfPairsJava9.java
public class ListOfPairsJava9 {
public static void main(String[] args) {
final Map<String, List<String>> values = new HashMap<>();
values.put("a", Arrays.asList("1", "2", "3"));
values.put("b", Arrays.asList("4", "5", "6"));
values.put("c", Collections.singletonList("7"));
final List<Map.Entry<String, String>> result = values
.entrySet()
You can’t perform that action at this time.