[英]Populating complex Map and accessing its values
Problem 1: I want to add "specific" sorted values of the following "Class Dog" into the "Map" used in the Class "Ser". 问题1:我想将以下“类狗”的“特定”排序值添加到“ 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 问题2:如果成功填充地图,如何访问地图的填充值
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 输出: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] 映射值的输出:[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,狗@ e7b241,狗@ 167d940,狗@ e83912,狗@ 1fae3c6]
You're seeing the Object.toString
representation of Dog
, you need to override that method: 您正在看到
Dog
的Object.toString
表示形式,您需要重写该方法:
@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. 您已经创建了一个
Map
对象,该对象创建一个Integer Key并将其映射到Dogs 列表 。 If you are wishing to map Integers to Dogs you need to start off with this: 如果您希望将Integers映射到Dogs,则需要从以下开始:
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. 现在,假设你的排序是工作(我还没有看进去,我就让你调试证明,如果它是与否),你需要使用
.get(int)
的方法List
收集来从狗您要放入地图中的列表。
What you are doing right now is mapping different integers to the same list 3 times. 您现在正在做的是将不同的整数映射到同一列表3次。
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. 0表示
list1
列表中的第一个Dog对象,请选择所需的内容代替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. 现在,对于需要打印它们的问题2 ,您将必须像Reimeus所说的那样滚动
toString()
函数或手动执行。
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. 同样,您之前所做的是获取已映射到映射中整数的
List
对象。 If you were to keep that format you'd have to then iterate over each value in the List: 如果要保留该格式,则必须遍历List中的每个值:
for (List<Dog> ld : map.values()) {
for (Dog d : ld) {
System.out.println(...);
}
}
I hope this helped you. 希望这对您有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.