Search for the middle field of an object for each key in the map

I currently have a map (shown below) <Object, ArrayList<Object>>. It contains an object observatory, which is associated with an array of objects of earthquake objects. What I'm going to do is find the average value of the earthquake attribute “magnitude” in each ArrayList array, returning the average value for each key (observatory).

  public static ArrayList<Observatory> obsList = new ArrayList<>();
  public static ArrayList<Earthquake> quakeList = new ArrayList<>();

  public static Map<Observatory, ArrayList<Earthquake>> eqMap = new HashMap<>();

Then I hope to return the name of the Observatory, which has the greatest value for the average value.

I populate my ArrayList using the following code:

    public static void mapQuakeToObs(String o, Earthquake e) {

          //Earthquake object contains String "o" to define its assigned observatory

        for (Observatory obs : obsList) {

            if(obs.getObsname().equals(o)) {

                Observatory x = obs;

                if (!eqMap.containsKey(o)) { 
                    eqMap.put(x, new ArrayList<Earthquake>()); 
                }
                eqMap.get(x).add(e);

            }                                               
        }           
    }

So, I'm looking for a way to calculate the average magnitude of earthquakes assigned to each key of the Observatory, returning the observatory key with the highest average value.

+4
1

, , , :

Earthquake:

    public class Earthquake {

    private final String obsname;
    private final float magnitude;

    public Earthquake(String obsname, float magnitude) {
        this.obsname = obsname;
        this.magnitude = magnitude;
    }

    public float getMagnitude() {
        return magnitude;
    }

    public String getObsname() {
        return obsname;
    }
}

:

public class Observatory {

    private final String obsname;

    public Observatory(String obsname) {
        this.obsname = obsname;
    }

    public String getObsname() {
        return obsname;
    }

    @Override
    public String toString() {
        return obsname;
    }
}

, :

public static float findAverageMagnitude(List<Earthquake> earthquakes) {
    float total = 0.0f;
    for(Earthquake earthquake : earthquakes) {
        total += earthquake.getMagnitude();
    }
    return total / earthquakes.size();
}

, , , :

    public static Map<String, List<Earthquake>> mapQuakeToObs(
            List<Observatory> obsList, List<Earthquake> quakes) {

        Map<String, List<Earthquake>> obsAndQuakes =
                new HashMap<String, List<Earthquake>>();
        //Earthquake object contains String "o" to define its assigned observatory
        for(Earthquake quake : quakes) {
            for (Observatory obs : obsList) {

                if (obs.getObsname().equals(quake.getObsname())) {
                    List<Earthquake> quakesMappedToObs = null;
                    // If the map doesn't already contain the key, then put it there
                    if(!obsAndQuakes.containsKey(obs.getObsname())) {
                        quakesMappedToObs = new ArrayList<Earthquake>();
                        obsAndQuakes.put(obs.getObsname(), quakesMappedToObs);
                    } else {
                        quakesMappedToObs = obsAndQuakes.get(obs.getObsname());
                    }
                    quakesMappedToObs.add(quake);
                }
            }
        }
        return obsAndQuakes;
    }

, , , , :

public static Observatory findObservatoryWithHighestAverageMagnitude(
            List<Observatory> observatories, List<Earthquake> earthquakes) {

        float highestRecordedAverage = 0.0f;
        String obsnameWithHighestRecordedAverage = null;
        Map<String, List<Earthquake>> obsAndQuakes
                = mapQuakeToObs(observatories, earthquakes);
        for(String obsName: obsAndQuakes.keySet()) {
            List<Earthquake> quakesMappedToObs = obsAndQuakes.get(obsName);
            float averageMagnitude = findAverageMagnitude(quakesMappedToObs);
            if(averageMagnitude > highestRecordedAverage) {
                highestRecordedAverage = averageMagnitude;
                obsnameWithHighestRecordedAverage = obsName;
            }

        }
        for(Observatory observatory : observatories) {
            if(observatory.getObsname().equals(obsnameWithHighestRecordedAverage)) {
                return observatory;
            }
        }
        //This code won't be reached
        return null;
    }

:

public static void main(String[] args) {
        Observatory observatory1 = new Observatory("Observatory One");
        Observatory observatory2 = new Observatory("Observatory Two");
        Earthquake quake1a = new Earthquake("Observatory One", 0.1f);
        Earthquake quake1b = new Earthquake("Observatory One", 7.9f);
        Earthquake quake1c = new Earthquake("Observatory One", 8.3f);
        Earthquake quake2a = new Earthquake("Observatory Two", 3.2f);
        Earthquake quake2b = new Earthquake("Observatory Two", 2.9f);
        Earthquake quake2c = new Earthquake("Observatory Two", 4.7f);
        List<Observatory> observatories = new ArrayList<Observatory>();
        observatories.add(observatory1);
        observatories.add(observatory2);
        List<Earthquake> earthquakes = new ArrayList<Earthquake>();
        earthquakes.add(quake1a);
        earthquakes.add(quake1b);
        earthquakes.add(quake1c);
        earthquakes.add(quake2a);
        earthquakes.add(quake2b);
        earthquakes.add(quake2c);
        // Prints "Observatory One"
        System.out.println(findObservatoryWithHighestAverageMagnitude(
                observatories, earthquakes));
    }
+3

All Articles