Java 列出共享相同属性的对象

Java 列出共享相同属性的对象,java,Java,好的,基本上我是在我的 private ArrayList<Temperatures> recordedTemperature; 如何遍历温度数组列表中的所有对象,找出具有匹配位置属性的对象并返回它们?您需要循环遍历列表,并根据您的条件验证列表中的每个项目。在您的情况下,需要传递列表并标识所有唯一的位置(例如,将它们放在地图中),并为每个位置添加具有该位置的条目列表 Map<Integer, List<Temperatures>> tempsByLocati

好的,基本上我是在我的

private ArrayList<Temperatures> recordedTemperature;

如何遍历温度数组列表中的所有对象,找出具有匹配位置属性的对象并返回它们?

您需要循环遍历列表,并根据您的条件验证列表中的每个项目。在您的情况下,需要传递列表并标识所有唯一的位置(例如,将它们放在地图中),并为每个位置添加具有该位置的条目列表

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
}
Map tempsByLocation=newhashmap();
温度(t:记录的温度){
//1检查是否存在这样的位置
//2如果已经存在,则将您的位置附加到该位置的列表中
//3否则,创建新密钥(新位置)并将仅包含您的温度的新列表添加到其中
}
您可以尝试:

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
}
Map Map=newhashmap()//为具有此位置的所有位置=>温度对象数组创建贴图
温度(t:记录的温度){
if(map.get(t.location)==null){
map.put(t.location,[]);//如果它是具有该位置的第一个对象,请为此位置添加一个新数组
}
map.put(t.location,map.get(t.location).push(t));//使用此位置获取温度并附加新的温度对象
}
然后迭代这些映射以获得所有组:

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(Map.Entry>Entry:Map.entrySet())
{
//entry.getKey()是位置
//getValue()是具有此位置的对象数组
}

请注意,我并没有实现和尝试这个,但它可能会工作或给你一个想法

如果您试图根据给定的
位置获取所有
温度
,您可以在Java 8中执行类似操作:

public List<Temperatures> getTemperaturesFromLocation(List<Temperatures> temperatures, int location) {
    return temperatures
           .stream()
           .filter(t -> 
               t.getLocation() == location
           )
           .collect(Collectors.toList());
}
public List getTemperaturesFromLocation(列出温度,int位置){
返回温度
.stream()
.过滤器(t->
t、 getLocation()==位置
)
.collect(Collectors.toList());
}
或使用常规循环/if语句:

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;
}
public List getTemperaturesFromLocation(列出温度,int位置){
List toReturn=new ArrayList();
用于(温度:温度){
if(temperature.getLocation()==位置){
t返回。添加(温度);
}
}
回归回归;
}

您可以使用Java 8和streams

要筛选
列表
请使用

List filtered=recordedTemperature.stream();
按位置和用途分组

Map group=recordedTemperature.stream().collect(Collectors.groupingBy(Temperature::getLocation));

您将得到
Map
,其中键是您的位置,值是具有给定位置的
温度的列表。

流对您来说可能太复杂了,但使用for循环和if语句。非常直截了当,一旦你知道了基本原理,就更容易理解了。帖子:是的,我想它必须是一个for循环或类似的东西。但是我的意思是,我怎么用int来做呢?我有点困惑,因为对于字符串,我会使用if.contains(searchString),但我不确定如何使用Int。如果没有字符串或Int的列表,那么就有
温度
对象。您必须执行
包含(someTemperature)
,但这需要的代码比这里需要的更多
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
}
List<Temperature> filtered = recordedTemperature.stream().filter(t -> t.getLocation() == 1).collect(Collectors.toList());
Map<Integer, List<Temperature>> grouped = recordedTemperature.stream().collect(Collectors.groupingBy(Temperature::getLocation));