简体   繁体   English

如何从哈希图返回对象

[英]how to return objects from hashmap

Car c1 = new Car();
Car c2 = new Car();

HashMap<String,Car> hm= new HashMap<String, Car>();
hm.put("Ford",c1);
hm.put("Volvo",c2);

How do I iterate to get only the values(only name) to be printed? 如何迭代只获取要打印的值(仅名称)?

Out should be: 出应该是:
c1 C1
c2 C2

Not the below : 不是以下内容:
c1@13efr5t4 C1 @ 13efr5t4
c2@234fvdf4 C2 @ 234fvdf4

Step 1: First you have to override the toString() method in the Car class. 步骤1:首先,您必须重写Car类中的toString()方法。

public class Car {
    // attribute
    private final String name;

    // Constructor
    public Car(final String name) {
        this.name = name;
    }

    // getter
    public String getName() {
        return name;
    }

    // Override of toString
    @Override
    public String toString() {
        return name;
    }
}

If you don't implement a proper toString -method the method from Object will be used when you invoke System.out.println(car) , and that implementation returns the following (which is what you see in your current printing): 如果未实现适当的toString方法,则在调用System.out.println(car)时将使用Object的方法,该实现将返回以下内容(这是您在当前打印中看到的内容):

return getClass().getName() + "@" + Integer.toHexString(hashCode());

The way you create a new Car from the class above is to invoke the following constructor: 从上面的类创建新Car的方法是调用以下构造函数:

Car c = new Car("Ford");

Step 2 : iterate using a loop. 步骤2 :使用循环进行迭代。 When using a Map you can choose to iterate over the keys , the values or the entries . 使用Map您可以选择遍历条目 All of these three alternatives returns some kind of Collection . 所有这三种选择都返回某种Collection Collections can be iterated using various types of loops. 可以使用各种类型的循环来迭代Collections

// Standard Java 5+ foreach-loop that prints the values
for (Car c : hm.values()) {
    System.out.println(c);
}

// Loop using an iterator that prints the keys
for (Iterator<Car> itr = hm.keys().iterator(); itr.hasNext(); ) {
    System.out.println(itr.next());
}

// Or a Java 8 loop
hm.values().forEach(System.out::println);

If you instead want the keys of the map ("Ford", "Volvo") you can replace the call to values() with a call to keySet() . 如果您想要地图的键(“福特”,“沃尔沃”),则可以将对values()的调用替换为对keySet()的调用。 For the entries, invoke the method entrySet() which returns a Map.Entry object where you can get both the key (via getKey() and the value via getValue() ). 对于条目,调用方法entrySet() ,该方法返回一个Map.Entry对象,您可以在其中同时获取键(通过getKey()和通过getValue()获得值)。

HashMap<String,Car> hm= new HashMap<String, Car>();
hm.put("Ford",c1);
hm.put("Volvo",c2);

In your hash map you are putting objects not string. 在哈希图中,您要放置的对象不是字符串。

When you pare printing your objects as string it is geving you output 当您将对象打印为字符串时,它会产生输出

c1@13efr5t4
c2@234fvdf4

If you have want to print suppose car name then use following way or you have to implement toString() method in your Car which will give you your expected output. 如果您要打印假设的汽车名称,请使用以下方法,否则您必须在Car实现toString()方法,这将为您提供预期的输出。

for (Car c : hm.values()) {
    System.out.println(c.getCarName());
}

There is no return directly you can use like that 没有直接的回报,你可以这样使用

for (Car c : hm.values()) {
System.out.printf(c);} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM