【问题标题】:Listing Objects that share the same attribute [duplicate]列出共享相同属性的对象[重复]
【发布时间】:2017-03-31 16:33:41
【问题描述】:

好的,所以基本上我正在尝试遍历我的对象中的

private ArrayList<Temperatures> recordedTemperature;

并显示共享相同“位置”的每个人。位置是在 Temperatures 类的构造函数中初始化的 int 变量:

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

如何遍历 Temperatures ArrayList 中的所有对象并找出具有匹配位置属性的对象并返回它们?

【问题讨论】:

  • 流对你来说可能太复杂了,但是使用 for 循环和 if 语句。一旦你了解了基础知识就非常简单
  • 是的,我认为它必须是一个 for 循环或类似的东西。但我的意思是我将如何使用 int 来做到这一点?我有点困惑,因为对于字符串我会使用 if.contains(searchString),但我不确定如何使用 ints 来实现
  • 您没有字符串或整数列表,您有 Temperatures 对象。你必须做contains(someTemperature),但这需要比这里需要更多的代码

标签: java


【解决方案1】:

您可以使用 Java 8 和流。

要过滤List,请使用filter

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

要按位置分组,请使用 collectgroupingBy

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

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

【讨论】:

    【解决方案2】:

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

    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
    }
    

    【讨论】:

      【解决方案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
      }
      

      然后遍历这些地图以获取所有组:

      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
      }
      

      请注意,我没有实施和尝试这个,但它可能会起作用或给你一个想法。

      【讨论】:

        【解决方案4】:

        如果您尝试根据给定的 location 获取所有 temperatures,您可以在 Java 8 中执行类似的操作:

        public List<Temperatures> getTemperaturesFromLocation(List<Temperatures> temperatures, int location) {
            return temperatures
                   .stream()
                   .filter(t -> 
                       t.getLocation() == location
                   )
                   .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;
        }
        

        【讨论】:

          猜你喜欢
          • 2015-11-21
          • 2021-10-12
          • 2011-02-10
          • 2023-02-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-02-14
          相关资源
          最近更新 更多