|
10 | 10 |
|
11 | 11 | public class Histogram {
|
12 | 12 |
|
| 13 | + private Map distributionMap; |
| 14 | + private int classWidth; |
| 15 | + |
13 | 16 | public Histogram() {
|
14 | 17 |
|
| 18 | + distributionMap = new TreeMap(); |
| 19 | + classWidth = 10; |
15 | 20 | Map distributionMap = processRawData();
|
16 | 21 | List yData = new ArrayList();
|
17 | 22 | yData.addAll(distributionMap.values());
|
@@ -46,43 +51,43 @@ private Map processRawData() {
|
46 | 51 | Frequency frequency = new Frequency();
|
47 | 52 | datasetList.forEach(d -> frequency.addValue(Double.parseDouble(d.toString())));
|
48 | 53 |
|
49 |
| - int classWidth = 10; |
50 |
| - |
51 |
| - Map distributionMap = new TreeMap(); |
52 | 54 | List processed = new ArrayList();
|
53 | 55 | datasetList.forEach(d -> {
|
| 56 | + double observation = Double.parseDouble(d.toString()); |
54 | 57 |
|
55 |
| - double observation = Double.parseDouble(d.toString()); |
56 |
| - |
57 |
| - if(processed.contains(observation)) |
58 |
| - return; |
59 |
| - |
60 |
| - long observationFrequency = frequency.getCount(observation); |
61 |
| - int upperBoundary = (observation > classWidth) ? Math.multiplyExact( (int) Math.ceil(observation / classWidth), classWidth) : classWidth; |
62 |
| - int lowerBoundary = (upperBoundary > classWidth) ? Math.subtractExact(upperBoundary, classWidth) : 0; |
63 |
| - String bin = lowerBoundary + "-" + upperBoundary; |
| 58 | + if(processed.contains(observation)) |
| 59 | + return; |
64 | 60 |
|
65 |
| - int prevUpperBoundary = lowerBoundary; |
66 |
| - int prevLowerBoundary = (lowerBoundary > classWidth) ? lowerBoundary - classWidth : 0; |
67 |
| - String prevBin = prevLowerBoundary + "-" + prevUpperBoundary; |
68 |
| - if(!distributionMap.containsKey(prevBin)) |
69 |
| - distributionMap.put(prevBin, 0); |
| 61 | + long observationFrequency = frequency.getCount(observation); |
| 62 | + int upperBoundary = (observation > classWidth) ? Math.multiplyExact( (int) Math.ceil(observation / classWidth), classWidth) : classWidth; |
| 63 | + int lowerBoundary = (upperBoundary > classWidth) ? Math.subtractExact(upperBoundary, classWidth) : 0; |
| 64 | + String bin = lowerBoundary + "-" + upperBoundary; |
70 | 65 |
|
71 |
| - if(!distributionMap.containsKey(bin)) { |
72 |
| - distributionMap.put(bin, observationFrequency); |
73 |
| - } |
74 |
| - else { |
75 |
| - long oldFrequency = Long.parseLong(distributionMap.get(bin).toString()); |
76 |
| - distributionMap.replace(bin, oldFrequency + observationFrequency); |
77 |
| - } |
| 66 | + updateDistributionMap(lowerBoundary, bin, observationFrequency); |
78 | 67 |
|
79 |
| - processed.add(observation); |
| 68 | + processed.add(observation); |
80 | 69 |
|
81 | 70 | });
|
82 | 71 |
|
83 | 72 | return distributionMap;
|
84 | 73 | }
|
85 | 74 |
|
| 75 | + private void updateDistributionMap(int lowerBoundary, String bin, long observationFrequency) { |
| 76 | + |
| 77 | + int prevLowerBoundary = (lowerBoundary > classWidth) ? lowerBoundary - classWidth : 0; |
| 78 | + String prevBin = prevLowerBoundary + "-" + lowerBoundary; |
| 79 | + if(!distributionMap.containsKey(prevBin)) |
| 80 | + distributionMap.put(prevBin, 0); |
| 81 | + |
| 82 | + if(!distributionMap.containsKey(bin)) { |
| 83 | + distributionMap.put(bin, observationFrequency); |
| 84 | + } |
| 85 | + else { |
| 86 | + long oldFrequency = Long.parseLong(distributionMap.get(bin).toString()); |
| 87 | + distributionMap.replace(bin, oldFrequency + observationFrequency); |
| 88 | + } |
| 89 | + } |
| 90 | + |
86 | 91 | public static void main(String[] args) {
|
87 | 92 | new Histogram();
|
88 | 93 | }
|
|
0 commit comments