I have two classes Dog.java
and DogSerach.java
and I want to print dog detail using HashMap. I studied duplicate of this question get string value from HashMap depending on key name and also studied Oracle doc http://docs.oracle.com/javase/tutorial/collections/interfaces/map.html , but still can not figure it out.
I have tried in DogSearch.java
for (String key: dogs.keySet()) {
System.out.println("Registration number : " + key);
System.out.println("Detail : " + dogs.get(key));
}
But I get
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]
I want to print like this
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 + "]";
}
}
You can just do:
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());
}
}
With Java 8 streams you can sort the map by the names of your dogs.
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));
and then loop through the result map.
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());
}
}
And you need a Comparator class
public class MyComparator implements Comparator<Dog>{
public int compare(Dog s1, Dog s2) {
return s1.getName().compareTo(s2.getName());
}
}
There are multiple ways to achieve what you want:
The first one is to change the toString()
method of your Dog.java
. When using System.out.println()
, java uses the toString()
method when passing classes as arguments. So changing your toString()
to :
return "Name: " + name + "\n" +
"Breed: " + breed;
should do the trick.
The second way is to change what you print in your for loop. an example of what you can do is:
for (String key: dogs.keySet()) {
System.out.println("Registration number : " + key);
Dog dog = dogs.get(key);
System.out.println("Name : " + dog.getName());
System.out.println("Breed: " + dog.getBreed());
}
why don't you create a method in class Dog and write the implementation the way would like to print!
So here you just need
for (String key: dogs.keySet()) {
System.out.println(dogs.get(key).getPrintString());
}
This will go in Dog class
public String getPrintString() {
return "Registration number : "+ getRegistrationNumber() +"\nName: " +getName() +"\nBreed:"+getBreed(); // This can be optimized further using StringBuffer
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.