Skip to content

How to filter an ArrayList that has multi members in each element?

I have a function that reads air pollution data from a file and save it to an Array List. I’d like to filter the elements and print out the result.

I’m able to get a row from the ArrayList with System.out.println("1st element " + pollution.get(0)) but don’t know how to apply a filter to a single column.

The data file looks like this

shanghai        2015-321   15   15   93.8   16
beijing         2015-332   23   270   86   -1

The AirPollution class

public class AirPollution {

    private String city;
    private String date;
    private int hour;
    private double pm;             // Particulate matter
    private double humidity;
    private double temperature;

    /** Construct a new AirPollution object */
    public AirPollution(String city, String date, int hour, double pm, double humidity, double temperature) {
        this.city = city;
        this.date = date;
        this.hour = hour;
        this.pm = pm;
        this.humidity = humidity;
        this.temperature = temperature;
    }

    /** Get the PM concentration */
    public double getPM() {return this.pm;}

    public String toString() {
    return this.city + " at " + this.hour + " on " + this.date
    + " Humidity: " + this.humidity + " temperature: " + this.temperature;
    }
}

Function that read the file and save it to an Array List

public class AirPollutionAnalyser {
    private ArrayList<AirPollution> pollution = new ArrayList<AirPollution>();

    public void loadData() {
       try {
           this.pollution.clear();
           List<String> pollution = Files.readAllLines(Path.of(UIFileChooser.open("pollution.txt")));
           for (String line : pollution) {
               Scanner s = new Scanner(line);
               String city = s.next();
               String date = s.next();
               int hour = s.nextInt();
               double pm = s.nextDouble();
               double humidity = s.nextDouble();
               double temperature = s.nextDouble();
               this.pollution.add(new AirPollution(city, date, hour, pm, humidity, temperature));
            }
           
       } catch(IOException e){UI.println("File reading failed");}
    }

Print out all the records in the ArrayList that have a PM2.5 concentration 300 and over.

** This is the function I’d like to make changes to.**

    public void findHazardousLevels() {
        UI.clearText();
        UI.println("PM2.5 Concentration 300 and above:");
        UI.println("------------------------");
        System.out.println("1st element " + pollution.get(0))
    }
}

How to achieve the result with minimal changes to the current code ? Thanks

Answer

It isn’t clear from your class fields what PM2.5 is but you can print each entry that has a pm >= 300 like this. Since your class fields are private it presumes you have getters to obtain the fields.

public void findHazardousLevels() {
    UI.clearText();
    UI.println("PM2.5 Concentration 300 and above:");
    UI.println("------------------------");
    for(AirPollution p : pollution) {
       if (p.getPm() >= 300) { 
           System.out.println(p);
       }
    }
}

Note: Your toString() implementation does not have pm included in the returned string.