Created
November 20, 2013 05:48
-
-
Save hoffrocket/7558352 to your computer and use it in GitHub Desktop.
Java 8 Histogram
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Map; | |
import java.util.Random; | |
import java.util.function.Function; | |
import java.util.stream.Collectors; | |
import java.util.stream.Stream; | |
public class Histogram { | |
public static <T> Map<T, Integer> histogram(Stream<T> stream) { | |
return stream.collect( | |
Collectors.groupingBy(Function.<T>identity(), | |
Collectors.mapping(i -> 1, Collectors.summingInt(s -> s.intValue())))); | |
} | |
public static void main(String [] a) { | |
Map<Integer, Integer> histo = histogram(new Random().ints(1, 11).limit(10000).boxed()); | |
System.out.println(histo); | |
} | |
} |
This could be reduced to (values changed to Long):
public static <T> Map<T, Long> histogram(Stream<T> stream) {
return stream.collect(
Collectors.groupingBy(
Function.identity(),
Collectors.counting()
));
This will not be very useful with floating point numbers. And it does not allow to set the bin width, so it makes an histogram of bin of width 1. A little bit of more code you can get something more useful.
`
import java.util.;
import java.util.stream.;
/**
- Create a simple histogram from an array of numbers via stream
- @author edo
*/
public class Histogram{
private int nBins, N=0;
private double min = Double.POSITIVE_INFINITY , max = Double.NEGATIVE_INFINITY;;
private double binWidth;
public Histogram(){
}
public Histogram(int bins){
setNBins(bins);
}
public static void main(String[] args) {
/**
* Create an arraylist of random numbers normally distributed
*/
Random rn = new Random();
int limit = 1000_000;
DoubleStream s = DoubleStream.generate(() -> rn.nextGaussian());
List<Double> data = s
.limit(limit)
.collect(()-> new LinkedList<>(),
LinkedList::add,
LinkedList::addAll);
/**
* Create an Histogram with the stream API
*/
int nbins = 12; // number of bins in the histogram
DoubleSummaryStatistics ds = data.stream().limit(limit)
.collect(DoubleSummaryStatistics::new,
DoubleSummaryStatistics::accept,
DoubleSummaryStatistics::combine);
Histogram h = new Histogram();
h.setMax(ds.getMax());
h.setMin(ds.getMin());
h.setN((int)ds.getCount());
h.setNBins(12);
Map<Integer, Integer> histo8 = h.histogram(data.stream());
System.out.println(histo8);
}
public void setNBins(int n){
nBins = n;
if (max != 0 && min != 0){
binWidth = (max-min)/nBins;
}
}
public void setN(int n){N=n;}
public void setMin(double val){
min = val;
if (max != 0 && nBins != 0){
binWidth = (max-min)/nBins;
}
}
public void setMax(double val){
max = val;
if (max != 0 && nBins != 0){
binWidth = (max-min)/nBins;
}
}
public Map<Integer, Integer> histogram(Stream<Double> stream) {
return stream.collect(
Collectors.groupingBy(
(x) -> findBin(x),
Collectors.mapping(i -> 1,
Collectors.summingInt(s -> s.intValue()
))
));
}
public Integer findBin(Double datum){
int ret = 0;
if (datum == max) {
ret = nBins-1;
} else if (datum == min) {
ret = 0;
} else {
ret = (int)Math.floor((datum-min)/binWidth);
}
return new Integer(ret);
}
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
approximate equivalent in scala for comparison: