All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.lambda.query.Counter Maven / Gradle / Ivy

There is a newer version: 24.9.0
Show newest version
package org.lambda.query;

import org.lambda.functions.Function1;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

public class Counter
{
  private final Map counts = new HashMap<>();
  public  void countAll(Collection collection, Function1 extractor)
  {
    Query.select(collection, extractor).forEach(this::count);
  }
  public void count(T value)
  {
    Integer count = counts.computeIfAbsent(value, x -> 0) + 1;
    counts.put(value, count);
  }
  public T getMaxValue()
  {
    if (counts.isEmpty())
    { return null; }
    return Query.max(counts.entrySet(), Map.Entry::getValue).getKey();
  }
  public static  Out getMaxValue(Collection collection, Function1 extractor)
  {
    Counter counter = new Counter<>();
    counter.countAll(collection, extractor);
    return counter.getMaxValue();
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy