我有两个类Dog.java
和DogSerach.java
,我想使用HashMap打印狗的细节。我研究了这个问题的副本get string value from HashMap depending on key name,并研究了Oracle doc http://docs.oracle.com/javase/tutorial/collections/interfaces/map.html,但仍然无法弄明白。
我在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<String, Dog> dogs = new HashMap<String, Dog>();
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 :(得分:2)
你可以这样做:
for (String key: dogs.keySet()) {
System.out.println("Registration number : " + key);
System.out.println("Name: " + dogs.get(key).getName());
System.out.println("Breed: " + dogs.get(key).getBreed());
}
}
使用Java 8流,您可以按照狗的名字对地图进行排序。
Map<String, Dog> result = dogs.entrySet().stream()
.sorted(Map.Entry.comparingByValue(new MyComparator()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue,
(oldValue, newValue) -> oldValue, LinkedHashMap::new));
然后遍历结果图。
for (String key: result.keySet()) {
System.out.println("Registration number : " + key);
System.out.println("Name: " + dogs.get(key).getName());
System.out.println("Breed: " + dogs.get(key).getBreed());
}
}
你需要一个Comparator类
public class MyComparator implements Comparator<Dog>{
public int compare(Dog s1, Dog s2) {
return s1.getName().compareTo(s2.getName());
}
}
答案 1 :(得分:1)
有多种方法可以实现您的目标:
第一个是更改toString()
的{{1}}方法。使用Dog.java
时,java在将类作为参数传递时使用System.out.println()
方法。
因此,将toString()
更改为:
toString()
应该这样做。
第二种方法是更改你在for循环中打印的内容。 你可以做的一个例子是:
return "Name: " + name + "\n" +
"Breed: " + breed;
答案 2 :(得分:0)
为什么不在课程 Dog 中创建方法,并按照要打印的方式编写实现!
所以在这里你只需要
for (String key: dogs.keySet()) {
System.out.println(dogs.get(key).getPrintString());
}
这将进入 Dog 类
public String getPrintString() {
return "Registration number : "+ getRegistrationNumber() +"\nName: " +getName() +"\nBreed:"+getBreed(); // This can be optimized further using StringBuffer
}