2014-11-02 686 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();