Skip to content

Commit 5ef5e53

Browse files
SeunMattmaibin
authored andcommitted
Update to BAEL-1425 (eugenp#4399)
* added example code for Java mail * added examp code for BAEL-1425 * updated example code for BAEL-1425 * updated example code for BAEL-1425
1 parent fb8224d commit 5ef5e53

1 file changed

Lines changed: 30 additions & 25 deletions

File tree

‎libraries/src/main/java/com/baeldung/commons/math3/Histogram.java‎

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,13 @@
1010

1111
public class Histogram {
1212

13+
private Map distributionMap;
14+
private int classWidth;
15+
1316
public Histogram() {
1417

18+
distributionMap = new TreeMap();
19+
classWidth = 10;
1520
Map distributionMap = processRawData();
1621
List yData = new ArrayList();
1722
yData.addAll(distributionMap.values());
@@ -46,43 +51,43 @@ private Map processRawData() {
4651
Frequency frequency = new Frequency();
4752
datasetList.forEach(d -> frequency.addValue(Double.parseDouble(d.toString())));
4853

49-
int classWidth = 10;
50-
51-
Map distributionMap = new TreeMap();
5254
List processed = new ArrayList();
5355
datasetList.forEach(d -> {
56+
double observation = Double.parseDouble(d.toString());
5457

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;
6460

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;
7065

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);
7867

79-
processed.add(observation);
68+
processed.add(observation);
8069

8170
});
8271

8372
return distributionMap;
8473
}
8574

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+
8691
public static void main(String[] args) {
8792
new Histogram();
8893
}

0 commit comments

Comments
 (0)