java 打印hashmap值_从HashMap打印值列表

我在DogSearch.java中尝试过

for (String key: dogs.keySet()) {

System.out.println("Registration number : " + key);

System.out.println("Detail : " + dogs.get(key));

}

但我明白了

Registration number : 1003

Detail : Dog [name=Luca, breed=Labrador, registrationNumber=1003]

Registration number : 1002

Detail : Dog [name=Gracie, breed=Rottweiler, registrationNumber=1002]

Registration number : 1001

Detail : Dog [name=Max, breed=German Shepherd, registrationNumber=1001]

我想像这样打印

Registration number: 1001

Name: Max

Breed: German Shepherd

... etc.

DogSearch.java

public class DogSearch {

static Scanner scanner = new Scanner(System.in);

public static void main(String[] args) {

Map dogs = new HashMap();

Dog max = new Dog("Max", "German Shepherd", "1001");

Dog gracie = new Dog("Gracie", "Rottweiler", "1002");

Dog luca = new Dog("Luca", "Labrador", "1003");

dogs.put(max.getRegistrationNumber(), max);

dogs.put(gracie.getRegistrationNumber(), gracie);

dogs.put(luca.getRegistrationNumber(), luca);

System.out.println("List of dogs by name: ");

for (String key: dogs.keySet()) {

System.out.println("Registration number : " + key);

System.out.println("Breed : " + dogs.get(key));

}

}

}

Dog.java

class Dog {

private String name;

private String breed;

private String registrationNumber;

public Dog(String name, String breed, String registrationNumber) {

this.name = name;

this.breed = breed;

this.registrationNumber = registrationNumber;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getBreed() {

return breed;

}

public void setBreed(String breed) {

this.breed = breed;

}

public String getRegistrationNumber() {

return registrationNumber;

}

public void setRegistrationNumber(String registrationNumber) {

this.registrationNumber = registrationNumber;

}

@Override

public int hashCode() {

final int prime = 31;

int result = 1;

result = prime * result + ((breed == null) ? 0 : breed.hashCode());

result = prime * result + ((name == null) ? 0 : name.hashCode());

result = prime * result + ((registrationNumber == null) ? 0 : registrationNumber.hashCode());

return result;

}

@Override

public boolean equals(Object obj) {

if (this == obj)

return true;

if (obj == null)

return false;

if (getClass() != obj.getClass())

return false;

Dog other = (Dog) obj;

if (breed == null) {

if (other.breed != null)

return false;

} else if (!breed.equals(other.breed))

return false;

if (name == null) {

if (other.name != null)

return false;

} else if (!name.equals(other.name))

return false;

if (registrationNumber == null) {

if (other.registrationNumber != null)

return false;

} else if (!registrationNumber.equals(other.registrationNumber))

return false;

return true;

}

@Override

public String toString() {

return "Dog [name=" + name + ", breed=" + breed + ", registrationNumber=" + registrationNumber + "]";

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值