如何在流 API Android Java 的 lambda 表达式中使用文件数组编写复杂的 if 条件

how to write complex if condition with File array in the lamba expression of stream API Android Java

我有以下代码

private ArrayList<String> findPhotos(Date startTimestamp, Date endTimestamp, String keywords, String longitude, String latitude) {
     
     ArrayList<String> photos = new ArrayList<String>();
     File[] fList = file.listFiles();
        if (fList != null) {
            for (File f : fList) {
                if (((startTimestamp == null && endTimestamp == null) || (f.lastModified() >= startTimestamp.getTime()
                        && f.lastModified() <= endTimestamp.getTime())
                ) && (keywords == "" || f.getPath().contains(keywords))
                        && (longitude == "" || f.getPath().contains(longitude))
                        && (latitude == "" || f.getPath().contains(latitude)))
                    photos.add(f.getPath());
            }
        }
    return photos
}

这个函数有五个参数:

  1. 开始时间
  2. 结束时间
  3. 关键词
  4. 经度
  5. 纬度

我过滤掉它们以获得唯一想要的结果。我想将此文件数组传输到 lambda 数组,然后执行以下代码。

Filter(startTimestamp == null && endTimestamp == null)

Filter(f -> f.lastModified() >= startTimestamp.getTime())

Filter(f -> f.getPath().contains(keywords) or (keywords == ""))

Filter(keywords == "" || f -> f.getPath().contains(keywords))

Filter(latitude== "" || f -> f.getPath().contains(latitude))
Filter(keywords == "" || f -> f.getPath().contains(keywords))

然后只获取路径,然后

.collect(Collectors.toList());

一旦我将它们从文件数组中过滤掉,然后只将文件路径添加到 Arraylist<String> 中。谁能帮我把这个变成 lambda 表达式?

您给定的方法存在几个问题:

  • 正如其他人已经提到的,lambda 的意义 expression就是要简短易懂
  • 得到你的多重逻辑的逻辑 您应该链接 filter(f -> ...) 调用的表达式。 这比将所有逻辑都放在一个巨人中要好 lambda 表达式。
  • 您需要了解 ==equals。所以你可能不是 keywords == "" 想要 keywords.equals("").
  • 您不能使用 ArrayList<String> 因为 .collect(Colllectors.toList()) 声明为 List<String>.

考虑到所有这些,您最终可能会得到这样的结果

private List<String> findPhotos(Date startTimestamp, Date endTimestamp, String keywords, String longitude, String latitude) {
{
    File[] fList = file.listFiles();
    if (fList == null)
        return new ArrayList<String>();
    List<String> photos = Arrays.stream(fList)
        .filter(f -> f.lastModified() >= startTimestamp.getTime())
        .filter(f -> f.getPath().contains(keywords))
        .filter(f -> keywords.equals("") || f.getPath().contains(keywords))
        .filter(f -> latitude.equals("") || f.getPath().contains(latitude))
        .filter(f -> keywords.equals("") || f.getPath().contains(keywords))
        .map(f -> f.getPath())  // convert File to String
        .collect(Collectors.toList());
    return photos;
}

我相信以下应该能达到您想要的结果。

private List<String> findPhotos(Date startTimestamp,
                                Date endTimestamp,
                                String keywords,
                                String longitude,
                                String latitude) {
    return java.util.Arrays.stream(new file.listFiles())
                           .filter(f -> startTimestamp == null || f.lastModified() >= startTimestamp.getTime())
                           .filter(f -> endTimestamp == null || f.lastModified() <= endTimestamp.getTime())
                           .map(file -> file.getPath())
                           .filter(path -> keywords.isEmpty() || path.contains(keywords))
                           .filter(path -> longitude.isEmpty() || path.contains(longitude))
                           .filter(path -> latitude.isEmpty() || path.contains(latitude))
                           .collect(Collectors.toList());
}
  • 方法 stream,在 class java.util.Arrays 中,将数组转换为流。在上面的代码中,该流将包含 class java.io.File.
  • 的实例
  • 方法 map,在接口 java.util.stream.Stream 中,returns 一个新的流,在上面的代码中,包含 String 个元素。
  • 最后将所有通过所有过滤器的路径收集到一个java.util.List.