2014-11-02 691 views
0

這是用BlueJ編碼的!用HashSet中的Collection.max()獲得最高值

嗨,大家好,我正在嘗試在HashSet中查找最大值。我試圖使用Collection.max方法,但它不適合我。我收到一個錯誤:找不到適用於max(int)的方法。錯誤發生在我的VerhuurderBedrijf類中,這是最後一種叫做getLongestRentPeriod的方法。

我有2個班,(它是荷蘭語)VerhuurderBedrijf和容器。 在Container類中,我有一個名爲differenceInDays的方法。這裏它計算startDate和endDate之間的天數差(請參閱Container類中的字段)。

在VerhuurderBedrijf中,我將Container對象添加到HashSet中。在我的getLongestRentPeriod中,我試圖找到在我的differenceInDays方法(Container類)中計算出的最高值。

可以說我在VerhuurderBedrijf類中添加了2個容器對象。
容器1 - startDate = 10-10-2014; endDate = 15-10-2014;
容器2 - startDate = 10-10-2014; endDate = 20-10-2014;
正如我所說的,在天差我differenceInDatys方法(Container類)計算/

所以我getLongestRentPeriode(verhuurderbedrijf類),我想獲得最高的價值,那爲11天(容器2 );

對不起,如果不清楚。我不善於解釋事物。

這裏是完整的代碼。

VerhuurderBedrijf類

import java.util.*; 
import java.text.DecimalFormat; 

public class VerhuurderBedrijf 
{ 
    private HashSet<Container> containerList; 
    // Decimalen object 
    DecimalFormat df; 
    /** 
    * Constructor for objects of class VerhuurderBedrijf 
    */ 
    public VerhuurderBedrijf() 
    { 
     containerList = new HashSet<Container>(); 
     // Dit zorgt ervoor dat er 2 decimalen achter de komma komen 
     df = new DecimalFormat("0.00"); 
    } 

    public void addContainer(Container container) 
    { 
     // Voegt een container toe een de HashSet<Container> 
     containerList.add(container); 
    } 

    public void gettotalIncome() 
    { 
     double total = 0; 
     for(Container container : containerList) 
     { 
      total += container.getIncome(); 
     } 
     System.out.println("The total amount of income for the rented containers are:\r\n " + (df.format(total)) + " euros.\r\n"); 
    } 

    public void getAverageVolume() 
    { 
     double average = 0; 
     double calc = 0; 
     for(Container container : containerList) 
     { 
      average += container.getVolume(); 
      calc = average/containerList.size(); 
     } 
     System.out.println("The average volume of the rented container are:\r\n " + (df.format(calc)) + ".\r\n"); 
    } 

    public void getLongestRentPeriod() 
    {  
     for(Container container : containerList) 
     { 
      System.out.println("max : " + Collections.max(container.differenceInDays())); 
     } 
    } 
} 

容器類

import java.text.SimpleDateFormat; 
import java.util.Date; 

public class Container 
{ 
    private int volume; 
    private String startDate; 
    private String endDate; 
    // Tijd object 
    SimpleDateFormat format; 

    public Container(int volume, String startDate, String endDate) 
    { 
     this.volume = volume; 
     this.startDate = startDate; 
     this.endDate = endDate; 
     // Dit zet de datum om naar een ddmmjjjj formaat, zo is het mogelijk om bijv: 12102014 in te vullen 
     format = new SimpleDateFormat("ddMMyyyy"); 
    } 

    public int getCosts() 
    { 
     int cost1 = 60; 
     int cost2 = 125; 
     if(volume <= 2) 
     { 
      // Als de volume kleiner of gelijk is aan 2, zijn de kosten voor de container 60 euro 
      return cost1; 
     } 
     else 
     { 
      // Anders zijn de kosten voor de container 125 euro 
      return cost2; 
     } 
    } 

    public int differenceInDays() 
    { 
     int startDateCheck = Integer.parseInt(startDate); 
     int endDateCheck = Integer.parseInt(endDate); 
     if(startDateCheck > endDateCheck) 
     { 
      // Als er een verkeerde invoer wordt gedaan 
      System.out.println("Your end date cannot be earlier than your start date"); 
     } 
     else 
     { 
      // Try & Catch omdat er een error ParseException komt, Date is een long in plaats van een String 
      try 
      { 
       String start = startDate; 
       String end = endDate; 

       Date day1 = format.parse(start); 
       Date day2 = format.parse(end); 

       long difference = day2.getTime() - day1.getTime(); 
       /* 
       Verschil in de dagen die hierboven wordt uitgerekend, wordt hier omgezet naar de aantal 
       dagen. 24 = uren, 60 = minuten, 60 = seconden, 1000 = milliseconden 
       */ 
       long differenceInDays = difference/(24 * 60 * 60 * 1000) + 1; 
       // Hier cast ik het naar een int, omdat het een public int methode is 
       int castToInt = (int) differenceInDays;  
       return castToInt; 
      } 
      catch(Exception e) 
      { 
       e.printStackTrace(); 
      } 
     } 
     return 0; 
    } 

    public int getIncome() 
    { 
     int rent = 40; 
     // (Huur container * de ingevoerde volume) + (Huur container * verschil in de dagen) 
     int income = (rent * volume) + (rent * differenceInDays()); 
     return income; 
    } 

    public int getVolume() 
    { 
     return volume; 
    } 
} 

我也嘗試過這一點,但它不工作要麼。我得到72的值,最新的數字不是正確的。

public void getLongestRentPeriod() 
{ 
    int temp = 0; 
    for(Container container : containerList) 
    { 
     if(temp < container.differenceInDays()) 
     { 
      temp = container.differenceInDays(); 
     } 
     //System.out.println("max : " + Collections.max(container.differenceInDays())); 
    } 
    System.out.println("max: " + temp);   
} 

任何人都可以給我一些提示或建議的解決方案?謝謝。

回答

2

錯誤消息說,這一切:

no suitable method found for max(int)

所以,你要使用的方法最大,以集合作爲參數,但你要傳遞的方法不是一個集合,它是一個INT:

Collections.max(container.differenceInDays()) 

differenceInDays()被定義爲

public int differenceInDays() 

我假定WH在你想要的是找到Container,在集containerList裏面,那是最大的差異。所以,你需要調用

Collections.max(containerList, comparatorComparingContainersByDifferenceInDays) 
+0

這就是我想要找什麼,但你的解決方案是不是爲我工作。它說:找不到符號變量comparatorComparingContainersByDifferenceInDays。我不太明白這行代碼壽。 – SC92 2014-11-02 16:06:54

+0

你應該寫一個比較器,它需要兩個容器,並通過它們的天差來比較它們。看看javadoc。 Google用於「比較Java中的對象」。看看Collections.max()的javadoc。 – 2014-11-02 17:10:57

0

您正在使用

Collections.max(container.differenceInDays()) 

如果你的最大方法將集合作爲parameter.see這http://docs.oracle.com/javase/7/docs/api/java/util/Collections.html#max%28java.util.Collection%29

所以,如果你讓你的集裝箱實現可比,如果你嘗試

Container container = Collections.max(containerList);//without for loop 

容器將容納所有容器的最大容量。或者,如果Container是第三方,我會建議你通過實現比較器接口使用比較方法創建靜態類(或匿名類)。像

private class ContainerComparator implements Comparator<Container> { 
    public int compare(Container o1, Container o2) { 
     //here calculate diff of each and send difference between the two 
    } 
} 
+0

我只需要獲取differenceInDays()的最大值,而不是containerList的最大值。 – SC92 2014-11-02 16:09:21

1

在Java 8,你可以做到以下幾點:

OptionalInt max = containerList.stream().mapToInt(Container::differenceInDays).max();