1

Okay, so basically I am trying to iterate through Objects in my

private ArrayList<Temperatures> recordedTemperature;

and display each one who shares the same "location". The location is an int variable initialized in the constructor of the Temperatures class:

public Temperatures(int location, int day, double temperature)
{
    this.location = location;
    this.day = day;
    this.temperature = temperature;
}

how would I iterate through all the objects in the Temperatures ArrayList and find out the ones who have a matching location attribute and return them?

Alec
  • 31,829
  • 7
  • 67
  • 114
  • 2
    Streams might be too complicated, for you, but use a for-loop and an if-statement. Very straight-forward once you know the basics – OneCricketeer Nov 17 '16 at 08:22
  • Easier to follow post: http://stackoverflow.com/questions/34506218/find-specific-object-in-a-list-by-attribute – OneCricketeer Nov 17 '16 at 08:25
  • Yes, I figured it has to be a for loop or something of the sort. But I mean how would I do it with an int? I am a bit confused, because for strings I would use the if.contains(searchString), but I am not sure of how to do it with ints – Florian Basta Nov 17 '16 at 08:26
  • You don't have a list of strings or ints, you have `Temperatures` objects. You would have to do `contains(someTemperature)`, but that requires more code than needed here – OneCricketeer Nov 17 '16 at 08:28

4 Answers4

2

You can use Java 8 and streams.

To filter List use filter

List<Temperature> filtered = recordedTemperature.stream().filter(t -> t.getLocation() == 1).collect(Collectors.toList());

To group by location use collect and groupingBy

Map<Integer, List<Temperature>> grouped = recordedTemperature.stream().collect(Collectors.groupingBy(Temperature::getLocation));

You will get Map where key is your location and value is a list of Temperature with the given location.

Mateusz
  • 690
  • 1
  • 6
  • 21
0

You need to loop through your list and verify each item in the list against your criteria. In your case, need to pass over the list and identify all unique locations (put them in a map for example) and for each location add the list of entries that have that location.

Map<Integer, List<Temperatures>> tempsByLocation = new HashMap<>();
for (Temperatures t : recordedTemperature) {
//1 check that there is such location
//2 if there is already, then append your location to the list at that location
//3 otherwise create the new key (new location) and add the new list containing only your temperature to it
}
ACV
  • 9,964
  • 5
  • 76
  • 81
0

You can try that:

Map<Integer, ArrayList<Temperatures>> map = new HashMap<Integer, ArrayList<Temperatures>>(); //create a map, for all location => array of Temperatures objects with this location
for(Temperatures t: recordedTemperatures){
    if(map.get(t.location)==null){
        map.put(t.location, []); // if it is first Temperatures object with that location, add a new array for this location
    }
    map.put(t.location, map.get(t.location).push(t)); // get the Temperatures with this location and append the new Temperatures object
}

Then iterate over these map to get all groups:

for (Map.Entry<Integer, ArrayList<Temperatures>>> entry : map.entrySet())
{
    // entry.getKey() is the location
    // entry.getValue() is the array of Temperatures objects with this location
}

Note that I didn't implement and try this, but it may work or give an idea to you.

Bünyamin Sarıgül
  • 3,031
  • 4
  • 30
  • 55
0

If you are trying to fetch all temperatures based on a given location you could do something like this in Java 8:

public List<Temperatures> getTemperaturesFromLocation(List<Temperatures> temperatures, int location) {
    return temperatures
           .stream()
           .filter(t -> 
               t.getLocation() == location
           )
           .collect(Collectors.toList());
}

Or with regular loop/if statement:

public List<Temperatures> getTemperaturesFromLocation(List<Temperatures> temperatures, int location) {
    List<Temperatures> toReturn = new ArrayList<>();
    for(Temperatures temperature : temperatures) {
        if(temperature.getLocation() == location) {
            toReturn.add(temperature);
        }
    }

    return toReturn;
}
Joakim
  • 126
  • 1
  • 11