你可以试试:
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
}
然后在这些地图迭代,以获得各组:
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
}
请注意,我并没有实现,并尝试这个,但它可能工作或给你一个想法。
流对于你来说可能太复杂了,但是使用for循环和if语句。非常直接,一旦你知道的基础 –
更容易遵循帖子:http://stackoverflow.com/questions/34506218/find-specific-object-in-a-list-by-attribute –
是的,我想它必须成为一个循环或类似的东西。但我的意思是如何用int来做到这一点?我有点困惑,因为对于字符串我会使用if.contains(searchString),但我不确定如何使用整数执行它 –