泛型和性能问题

时间:2010-05-02 21:47:51

标签: java generics performance

我想知道是否有人可以查看我编写的类,我在Eclipse中收到通用警告,我只是想知道它是否可以被清除。我收到的所有警告都包含在下面的代码中**中。

该类以(hh:mm AM / PM)的形式获取字符串列表,并将它们转换为HourMinute对象,以便在列表中找到当前时间之后的第一次。

我也很好奇是否有更有效的方法来做到这一点。这很好,但我的学生只是想知道如何做得更好。

public class FindTime {
    private String[] hourMinuteStringArray;

    public FindTime(String[] hourMinuteStringArray){
        this.hourMinuteStringArray = hourMinuteStringArray;
    }

    public int findTime(){

        HourMinuteList hourMinuteList = convertHMStringArrayToHMArray(hourMinuteStringArray);
        Calendar calendar = new GregorianCalendar();
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        int minute = calendar.get(Calendar.MINUTE);
        HourMinute now = new HourMinute(hour,minute);
        int nearestTimeIndex = findNearestTimeIndex(hourMinuteList, now);
        return nearestTimeIndex;
    }

    private int findNearestTimeIndex(HourMinuteList hourMinuteList, HourMinute now){
        HourMinute current;
        int position = 0;
        Iterator<HourMinute> iterator = **hourMinuteList.iterator()**;
        while(iterator.hasNext()){
            current = (HourMinute) iterator.next();
            if(now.compareTo(current) == -1){
                return position;
            }
            position++;
        }
        return position;
    }


    private static HourMinuteList convertHMStringArrayToHMArray(String[] times){
        FindTime s = new FindTime(new String[1]);
        HourMinuteList list = s.new HourMinuteList();
        String[] splitTime = new String[3];
        for(String time : times ){
            String[] tempFirst = time.split(":");
            String[] tempSecond = tempFirst[1].split(" ");
            splitTime[0] = tempFirst[0];
            splitTime[1] = tempSecond[0];
            splitTime[2] = tempSecond[1];
            int hour = Integer.parseInt(splitTime[0]);
            int minute = Integer.parseInt(splitTime[1]);
            HourMinute hm;
            if(splitTime[2] == "AM"){
                hm = s.new HourMinute(hour,minute);
            }
            else if((splitTime[2].equals("PM")) && (hour < 12)){
                hm = s.new HourMinute(hour + 12,minute);
            }
            else{
                hm = s.new HourMinute(hour,minute);
            }

            **list.add(hm);**
        }
        return list;
    }
    class **HourMinuteList** extends **ArrayList** implements RandomAccess{

    }
    class HourMinute implements **Comparable** {
        int hour;
        int minute;

        public HourMinute(int hour, int minute) {
            setHour(hour);
            setMinute(minute);
        }

        int getMinute() {
            return this.minute;
        }
        String getMinuteString(){
            if(this.minute < 10){
                return "0" + this.minute;
            }else{
                return "" + this.minute;
            }
        }

        int getHour() {
            return this.hour;
        }

        void setHour(int hour) {
            this.hour = hour;
        }

        void setMinute(int minute) {
            this.minute = minute;
        }

        @Override
        public int compareTo(Object aThat) {

            if (aThat instanceof HourMinute) {
                HourMinute that = (HourMinute) aThat;
                if (this.getHour() == that.getHour()) {
                    if (this.getMinute() > that.getMinute()) {
                        return 1;
                    } else if (this.getMinute() < that.getMinute()) {
                        return -1;
                    } else {
                        return 0;
                    }
                } else if (this.getHour() > that.getHour()) {
                    return 1;
                } else if (this.getHour() < that.getHour()) {
                    return -1;
                } else {
                    return 0;
                }
            }

            return 0;
        }

    }


If you have any questions let me know. 

Thanks, 
Rob

2 个答案:

答案 0 :(得分:5)

这是因为你没有为Lis​​t和Comparable实例指定泛型,它们可以支持泛型。您可以使用以下命令重写代码:

class HourMinuteList extends ArrayList<HourMinute> implements RandomAccess{

}
class HourMinute implements Comparable<HourMinute> {

   public int compareTo(HourMinute aThat) {
   ....
  }
}

注意:泛型不是必需的,也不是在运行时使用,但最好使用它们,因为它可以帮助您避免代码中的一些错误。

答案 1 :(得分:4)

我不会使用HourMinute类,除非它有一些其他附加值。如果您只需要在给定时间点之后找到最接近的事件时间,请将字符串转换为Date(或表示时间的长值),并将它们存储在某个已排序的集合中。 转换可以使用 SimpleDateFormat

完成

如果动态添加项目,请使用 TreeSet&lt; Date&gt; ,以及ceiling(t)/ higher(t)方法。

如果项目集不是动态的,请使用数组日期[] 以及Arrays.binarySearch(..)。

这是第一种方法的(工作)草案:

public class TimedEventsMgr {
    private TreeSet<Date> pointsInTime = new TreeSet<Date>();
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd hh:mm a");

    //timeStr: hh:mm AM/PM
    public void add(String timeStr) throws ParseException{
        Date time = sdf.parse("20000101 "+timeStr);
        pointsInTime.add(time);
    }

    public Date closestFutureTime(Date time){
        Calendar c = Calendar.getInstance();
        c.setTime(time);
        c.set(Calendar.YEAR, 2000);
        c.set(Calendar.MONTH, 0); //January
        c.set(Calendar.DATE, 1);
        return pointsInTime.higher(c.getTime());
    }
}