【问题标题】:Printing only one object from a HashMap仅从 HashMap 打印一个对象
【发布时间】:2012-12-21 04:02:51
【问题描述】:

我的代码如下所示:

public Flight{
String code;
char status;
char type;

   Flight(String code, char status, char type){
    this.code = code;
    this.status = status;
    this.type = type;
   }
 }


public Menu{
 Flight flight1 = new Flight("DL123",'A','D');
 Flight flight2 = new Flight("DL146",'A','I');
 flightMap.put("DL123", flight1)
 flightMap.put("DL146", flight2)
 }


  if(schedule.flightMap.containsKey(decision))
  {

  }

如果用户输入 DL123 并且 containsKey 返回 true,我想返回 only flight1 的对象属性。我怎么能做到这一点?我尝试过覆盖 toString,但是因为 toString 只能作为字符串返回,所以我不知道如何返回作为字符的状态和类型属性。

如果您需要更多信息,请询问!

【问题讨论】:

  • 假设 toString() 被您想要打印的任何内容覆盖,请从地图中获取并调用 toString()。
  • 如果您希望从单个函数返回所有属性,则需要将它们包装在单个对象中,因为函数只能返回一件事......所以你只是返回反正整个对象。对象的字段(或属性)并不独立于该对象而存在。
  • 请详细解释您的问题。当您调用 flightMap.get("DL123") 时,您正在使用字符串键在地图中添加飞行对象,例如“DL123”,它将自动返回与该键关联的对象。您是否只想返回一个属性或飞行对象。

标签: java object printing hashmap


【解决方案1】:

Flight 类中定义getter 方法,然后:

 if(schedule.flightMap.containsKey(decision)){
   Fligth matchingFlight = schedule.flightMap.get(decision);
   String code = matchingFlight.getCode();
   char status = matchingFlight.getStatus();
   char type = matchingFlight.getType();
 }

【讨论】:

  • @Brian 你的意思是,这有帮助吗?
  • @Brian 然后接受答案。人们会考虑回答其他问题。
【解决方案2】:

航班航班 = schedule.flightMap.get(decision);

然后从飞行对象中,您可以检索所有值

【讨论】:

    【解决方案3】:

    你需要的是

    Flight flight = schedule.flightMap.get(decision);
    

    使用这些您可以简单地访问对象,因为它们的默认可见性是这样的

    flight.code
    flight.status
    

    但更合乎道德的方式是像这样定义所有变量的 getter 和 setter

    public void setCode(String code)
    {
         this.code = code;
    }
    public String getCode()
    {
        return this.code;
    }
    

    这样你就可以使用 this 获取变量

    String code = flight.getCode();
    

    另请参阅

    Why use getters and setters in java

    【讨论】:

    【解决方案4】:

    我试图解决您的问题并得出结论。请参阅下面的代码。

    package com.rais;
    
    import java.util.HashMap;
    import java.util.Map;
    
    /**
     * @author Rais.Alam
     * @project Utils
     * @date Dec 21, 2012
     */
    
    
    public class FlightClient
    {
    /**
     * @param args
     */
    public static void main(String[] args)
    {
        Map<String,Flight> flightMaps = new HashMap<String, Flight>();
        Flight flight1 = new Flight("DL123", "STATUS-1", "TYPE-1");
        Flight flight2 = new Flight("DL124", "STATUS-2", "TYPE-2");
        Flight flight3 = new Flight("DL125", "STATUS-3", "TYPE-3");
    
        flightMaps.put("DL123", flight1);
        flightMaps.put("DL124", flight2);
        flightMaps.put("DL125", flight3);
    
        System.out.println(getValue(flightMaps, "DL123"));
    
    
    }
    
    
    public static String getValue(Map<String,Flight> flightMaps, String key)    
    {
        if(flightMaps !=null && flightMaps.containsKey(key))
        {
            return flightMaps.get(key).status;
        }
        else
        {
            throw new RuntimeException("Flight does not exists");
        }
    
    
        }
    }
    
        class Flight
        {
        String code;
        String status;
        String type;
        /**
         * @param code
         * @param status
         * @param type
         */
        public Flight(String code, String status, String type)
        {
            super();
            this.code = code;
            this.status = status;
            this.type = type;
        }
    
    
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-08
      • 1970-01-01
      • 2021-11-04
      相关资源
      最近更新 更多