Kaynağa Gözat

Fixed modus being slow as fuck

Kenneth van Ewijk 11 yıl önce
ebeveyn
işleme
d7364183b1
2 değiştirilmiş dosya ile 23 ekleme ve 16 silme
  1. 22 15
      StatisticsCalculator.java
  2. 1 1
      WindDirection.java

+ 22 - 15
StatisticsCalculator.java

@@ -1,7 +1,9 @@
-package weerstation1;
+ 
 import java.util.ArrayList;
 import java.util.Calendar;
 import java.util.Collections;
+import java.util.Map;
+import java.util.HashMap;
 
 public class StatisticsCalculator {
     
@@ -64,22 +66,27 @@ public class StatisticsCalculator {
     }
     
     public static double modus(ArrayList<Double> array){
-        double maxValue = 0;
-        int maxCount = 0;
-    
-        for (int i = 0; i < array.size(); ++i){             //cycle through every number in the array
-            int count = 0;
-            for (int j = 0; j < array.size(); ++j) {        
-                if (array.get(j) == array.get(i)){
-                    ++count; 
-                }
-            } 
-            if (count > maxCount) {                         //if the count is bigger then the max count, it will be the temporary modus
-                maxCount = count; 
-                maxValue = array.get(i); 
+        Map<Double,Integer> map = new HashMap<Double,Integer>();
+        for(double number : array)
+        {
+            if(map.containsKey(number)){
+                map.put(number,(int)map.get(number)+1);
+            }else{
+                map.put(number, 1);
+            }
+        }
+        
+        double maxVal = 0;
+        double maxKey = 0;
+        
+        for (Map.Entry<Double, Integer> entry : map.entrySet()) {
+            if (entry.getValue() > maxVal) {
+                maxVal = entry.getValue();
+                maxKey = entry.getKey();
             }
         }
-        return maxValue;
+        
+        return maxKey;
     }   
         
     public static double afwijking(ArrayList<Double> array){

+ 1 - 1
WindDirection.java

@@ -15,7 +15,7 @@ public class WindDirection extends Grootheid{
     public void updatePeriod(ArrayList<Measurement> measurement2){
         createList(measurement2);
         maxMin();
-        //modus();
+        modus();
     }
     
     public void displayGraph()