簡體   English   中英

填充復雜的Map並訪問其值

[英]Populating complex Map and accessing its values

問題1:我想將以下“類狗”的“特定”排序值添加到“ Ser”類中使用的“映射”中。 但我不知道如何實現這一目標,

問題2:如果成功填充地圖,如何訪問地圖的填充值

以下是我的絕望嘗試。

 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);
         }

   }

輸出: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

映射值的輸出:[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]

您正在看到DogObject.toString表示形式,您需要重寫該方法:

@Override
public String toString() {
    return "Dog [Price=" + Price + ", Operator=" + Operator + "]";
}

問題一:

您已經創建了一個Map對象,該對象創建一個Integer Key並將其映射到Dogs 列表 如果您希望將Integers映射到Dogs,則需要從以下開始:

Map <Integer, Dog> map= new HashMap <Integer, Dog> ();

現在,假設你的排序是工作(我還沒有看進去,我就讓你調試證明,如果它是與否),你需要使用.get(int)的方法List收集來狗您要放入地圖中的列表。

您現在正在做的是將不同的整數映射到同一列表3次。

map.put(92, list1.get(0));

0表示list1列表中的第一個Dog對象,請選擇所需的內容代替0。

現在,對於需要打印它們的問題2 ,您將必須像Reimeus所說的那樣滾動toString()函數或手動執行。

for (Dog d : map.values()) {
  System.out.println("Dog " + d.getPrice() + " " + d.getOperator());
}

同樣,您之前所做的是獲取已映射到映射中整數的List對象。 如果要保留該格式,則必須遍歷List中的每個值:

for (List<Dog> ld : map.values()) {
  for (Dog d : ld) {
    System.out.println(...);
  }
}

希望這對您有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM