diff options
| author | ivanklaric <ivanklaric@users.noreply.github.com> | 2024-01-13 22:02:36 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-01-13 22:02:36 +0100 |
| commit | 05fe91694851496c32b2ed0b79aeae951046a988 (patch) | |
| tree | de1181b32607dd78baf9b0cef0241a97f80204b8 /src/main | |
| parent | f379e651931e6616294f230a22ba283ac5872af6 (diff) | |
my 1brc entry (#367)
Diffstat (limited to 'src/main')
| -rw-r--r-- | src/main/java/dev/morling/onebrc/CalculateAverage_ivanklaric.java | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/src/main/java/dev/morling/onebrc/CalculateAverage_ivanklaric.java b/src/main/java/dev/morling/onebrc/CalculateAverage_ivanklaric.java new file mode 100644 index 0000000..c5d893d --- /dev/null +++ b/src/main/java/dev/morling/onebrc/CalculateAverage_ivanklaric.java @@ -0,0 +1,53 @@ +/* + * Copyright 2023 The original authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package dev.morling.onebrc; + +import java.io.IOException; +import java.util.concurrent.ConcurrentSkipListMap; +import java.util.concurrent.ConcurrentMap; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.stream.Stream; + +public class CalculateAverage_ivanklaric { + private static final String FILE = "measurements.txt"; + + record CityTemps (double min, double max, double sum, long count) { + public String toString() { + return round(min) + "/" + round(sum / count) + "/" + round(max); + } + + private double round(double num) { + return Math.round(num * 10.0) / 10.0; + } + } + + public static void main(String[] args) throws IOException { + Stream<String> lines = Files.lines(Paths.get(FILE)); + ConcurrentMap<String, CityTemps> cityStats = new ConcurrentSkipListMap<>(); + + lines.parallel().forEach(line -> { + int splitterLoc = line.indexOf(';'); + double temp = Double.parseDouble(line.substring(splitterLoc + 1, line.length())); + + cityStats.merge(line.substring(0, splitterLoc), new CityTemps(temp, temp, temp, 1), + (oldValue, defaultValue) -> { + return new CityTemps(Math.min(oldValue.min, temp), Math.max(oldValue.max, temp), oldValue.sum + temp, oldValue.count + 1); + }); + }); + System.out.println(cityStats); + } +} |
