如何在找不到条件时返回null?

时间:2017-09-20 07:57:00

标签: java linked-list

当我的方法找不到它想要的内容时,我试图获得返回null。该方法在截止日期(参数)之前查找最近提交的内容。有人可以帮助我在参数日期之前没有提交时如何获得这个。谢谢!

    // students is a LinkedList

    @Override
    public Submission getSubmissionBefore(String unikey, Date deadline) {
        // TODO Implement this, ideally in better than O(n)

        if (unikey == null || deadline == null) {
            throw new IllegalArgumentException("Unikey or deadline was null");
        }

        Date tempDate = null;
        int k = -1;

        for (int i = 0; i < this.students.size(); i++) {
            if (students.get(i).getUnikey().equals(unikey)) {
                if (tempDate == null) {
                    tempDate = students.get(i).getTime();
                    k = i;
                } else if (students.get(i).getTime().before(deadline) && students.get(i).getTime().after(tempDate)) {
                    k = i;
                }
            }
        }

        return students.get(k);
    }

5 个答案:

答案 0 :(得分:3)

您也可以functional方式使用流解决问题(当然,如果您使用的是java 8)。

public static Submission getSubmisionBefore(String unikey, Date deadline) {
    if (unikey == null || deadline == null) {
        throw new IllegalArgumentException("Unikey or deadline was null");
    }

    Optional<Submission> any = students.stream()
            .filter(submission -> submission.getUniKey().equals(unikey) && submission.getTime().before(deadline))
            .max(Comparator.comparing(Submission::getTime));

    return any.orElse(null);

}

答案 1 :(得分:3)

您的代码存在一些问题:

  • 您的第一个if条件错误;它会匹配任何提交的匹配unikey,即使它在截止日期之后
  • 您的第二个else if也是错误的,因为您设置了k,但未设置tempDate
  • return之前
  • ,您应该检查k是否仍为-1,例如使用三元return k == -1 ? null : students.get(k);但是:
  • 而不是迭代索引,您可以使用for-each循环直接迭代提交,使整个代码更紧凑
  • 通过存储结果本身,您既不需要k也不需要tempDate,也不必检查索引末尾是否仍为-1

使用上述建议,您可以简化循环:

Submission res = null;
for (Submission sub : this.students) {
    if (sub.getUnikey().equals(unikey)) {
        if (sub.getTime().before(deadline) && 
                (res == null || sub.getTime().after(res.getTime()))) {
            res = sub;
        }
    }
}
return res;

或者这种更紧凑的形式,使用Java 8 Streams:

return this.students.stream()                        // iterate all students
        .filter(s -> s.getUnikey().equals(unikey))   // get entries with matching key
        .filter(s -> s.getTime().before(deadline))   // get entries before deadline
        .max(Comparator.comparing(s -> s.getTime())) // from those, get the latest one
        .orElse(null);                               // or null if none exists

答案 2 :(得分:1)

public Submission getSubmissionBefore(String unikey, Date deadline) {
    // TODO Implement this, ideally in better than O(n)

    if (unikey == null || deadline == null) {
        throw new IllegalArgumentException("Unikey or deadline was null");
    }

    Submission sub = null;
    Date tempDate = null;

    for (int i = 0; i < this.students.size(); i++) {
        if (unikey.equals(students.get(i).getUnikey())) {
            if (tempDate == null) {
                tempDate = students.get(i).getTime();
                sub = students.get(i);
            } else if (students.get(i).getTime().before(deadline) && students.get(i).getTime().after(tempDate)) {
                sub = students.get(i);
            }
        }
    }

    return sub;
}

答案 3 :(得分:1)

你可以通过添加控件

来实现
@Override
        public Submission getSubmissionBefore(String unikey, Date deadline) {
            // TODO Implement this, ideally in better than O(n)

            if (unikey == null || deadline == null) {
                throw new IllegalArgumentException("Unikey or deadline was null");
            }

            Date tempDate = null;
            int k = -1;

            for (int i = 0; i < this.students.size(); i++) {
                if (students.get(i).getUnikey().equals(unikey)) {
                    if (tempDate == null) {
                        tempDate = students.get(i).getTime();
                        k = i;
                    } else if (students.get(i).getTime().before(deadline) && students.get(i).getTime().after(tempDate)) {
                        k = i;
                    }
                }
            }
            if(k!=-1)
            return students.get(k);
            else
            return null;
        }

答案 4 :(得分:1)

您可以比较当前日期并使用if语句

返回null
    Date current = new Date();
    if(current.before(deadline))
           return null;
    else{  for (int i = 0; i < this.students.size(); i++) {
        if (students.get(i).getUnikey().equals(unikey)) {
            if (tempDate == null) {
                tempDate = students.get(i).getTime();
                k = i;
            } else if (students.get(i).getTime().before(deadline) && students.get(i).getTime().after(tempDate)) {
                k = i;
            }
        }
    }

    return students.get(k); 
                   }