Problem 1: I want to add "specific" sorted values of the following "Class Dog" into the "Map" used in the Class "Ser". But i do not know how to achieve this,
Problem 2: How to Accessing the populated value of the map if it successfully populated
The following is my desperate Attempt.
class Dog implements Comparator<Dog>, Comparable<Dog>{
private double Price;
private String Operator;
Dog(){
}
Dog( double p, String o){
Price= p;
Operator=o;
}
public double getPrice(){
return Price;
}
public String getOperator(){
return Operator;
}
// Overriding the compareTo method
public int compareTo(Dog d){
double data= Price - d.Price;
if ( data > 0.00001)return 1;
if (data < 0.00001) return -1;
return 0;
}
// Overriding the compare method to sort the age
public int compare(Dog d, Dog d1){
double data= d.Price - d1.Price;
if ( data > 0.00001)return 1;
if (data < 0.00001) return -1;
return 0;
}
}
public class Ser {
/**
* @param args
*/
public static void main(String[] args) {
// Takes a list o Dog objects
ArrayList <Dog> list1 = new ArrayList<Dog>();
Map <Integer, ArrayList<Dog> > map= new HashMap <Integer, ArrayList<Dog> > ();
list1.add(new Dog(0.99 , "A"));
list1.add(new Dog(0.91 , "C"));
list1.add(new Dog(0.92 , "A"));
list1.add(new Dog(0.97 , "B"));
list1.add(new Dog( 0.93 , "C"));
list1.add(new Dog(0.97 , "B"));
list1.add(new Dog(0.92, "A"));
list1.add(new Dog(0.97, "C"));
list1.add(new Dog(0.92, "A"));
// Sorts the array list using comparator
Collections.sort(list1, new Dog());
for(Dog a: list1)//printing the sorted list of ages
System.out.println( a.getOperator()+" : "+ a.getPrice());
map.put(92, list1);
map.put(445, list1);
map.put(966, list1);
// Collections.sort(list1, new Dog());
for (ArrayList<Dog> key: map.values() )
System.out.println(key);
}
}
OutPut: C : 0.91, A : 0.92, A : 0.92, A : 0.92, C : 0.93, C : 0.97, B : 0.97, B : 0.97,A : 0.99
Output of Map Values: [Dog@a981ca, Dog@8814e9, Dog@1503a3, Dog@1a1c887, Dog@743399, Dog@e7b241, Dog@167d940, Dog@e83912, Dog@1fae3c6] [Dog@a981ca, Dog@8814e9, Dog@1503a3, Dog@1a1c887, Dog@743399, Dog@e7b241, Dog@167d940, Dog@e83912, Dog@1fae3c6] [Dog@a981ca, Dog@8814e9, Dog@1503a3, Dog@1a1c887, Dog@743399, Dog@e7b241, Dog@167d940, Dog@e83912, Dog@1fae3c6]
You're seeing the Object.toString
representation of Dog
, you need to override that method:
@Override
public String toString() {
return "Dog [Price=" + Price + ", Operator=" + Operator + "]";
}
Problem 1:
You've created a Map
object that creates an Integer Key and maps it to a list of Dogs. If you are wishing to map Integers to Dogs you need to start off with this:
Map <Integer, Dog> map= new HashMap <Integer, Dog> ();
Now, assuming your sort is working (I haven't looked into it, I'll let you debug to prove if it is or not) you need to use the .get(int)
method of the List
collection to fetch the dog from the list you wish to put into the map.
What you are doing right now is mapping different integers to the same list 3 times.
map.put(92, list1.get(0));
The 0 refers to the first Dog object in the list1
List, pick whatever you need in place of 0.
Now, for Problem 2 where you need to print them you'll have to roll a toString()
function like Reimeus said or do it manually.
for (Dog d : map.values()) {
System.out.println("Dog " + d.getPrice() + " " + d.getOperator());
}
Again, what you were doing previously was fetching the List
object you had mapped to integers in the map. If you were to keep that format you'd have to then iterate over each value in the List:
for (List<Dog> ld : map.values()) {
for (Dog d : ld) {
System.out.println(...);
}
}
I hope this helped you.
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.