java list.get不能用,为什么Java List接口不支持getLast()?

I'm trying to understand the API inconsistency in the Java standard collections library.

There is no method in List or in AbstractList to get the last item, although one can simulate that with size and getIndex().

However, LinkedList supports that function.

Any idea why it was decided not to support this method in the interface?

解决方案

The java.util.List interface doesn't support getLast() because the designers went for a 'minimal interface'. With the minimal number of methods defined it makes it both easier to understand and quicker to learn.

This is in contrast with a 'humane interface' (such as used in the Ruby array class) which attempts to provide methods for doing common operations (e.g. getLast()). As there are many uses which such a fundamental concept as a list can be put to this tends to lead to much larger interfaces.

For further information see Martin Fowler's Minimal Interface and Humane Interface descriptions.

As to why LinkedList supports getLast() etc., to quote the javadoc:

... the LinkedList class provides uniformly named methods to get, remove and insert an element at the beginning and end of the list. These operations allow linked lists to be used as a stack, queue, or double-ended queue (deque).

Presumably it was felt that a general List would not be adequate for these specific use cases.

As an insight into the mind of the principal designer of the Java Collections API (Joshua Bloch) he provides this list of API design maxims by which he works. Of which, the most pertinent to this question are:

Early drafts of APIs should be short, typically one page with class and method signatures and one-line descriptions. This makes it easy to restructure the API when you don't get it right the first time.

When in doubt, leave it out. If there is a fundamental theorem of API design, this is it. It applies equally to functionality, classes, methods, and parameters. Every facet of an API should be as small as possible, but no smaller. You can always add things later, but you can't take them away. Minimizing conceptual weight is more important than class- or method-count.

Keep APIs free of implementations details. They confuse users and inhibit the flexibility to evolve. It isn't always obvious what's an implementation detail: Be wary of overspecification.

Minimize accessibility; when in doubt, make it private. This simplifies APIs and reduces coupling.

Consider the performance consequences of API design decisions, but don't warp an API to achieve performance gains. Luckily, good APIs typically lend themselves to fast implementations.

However he also states:

Don't make the client do anything the library could do. Violating this rule leads to boilerplate code in the client, which is annoying and error-prone.

Which just shows that design guidelines often conflict and the hardest part of an API designers job is to balance these conflicts.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 可以使用Java 8的新特性,如Streams和Lambdas来实现。例如: ``` Path directory = ... List<Path> files = Files.list(directory) .filter(Files::isRegularFile) .sorted((o1, o2) -> -o1.toFile().lastModified() .compareTo(o2.toFile().lastModified())) .collect(Collectors.toList()); ``` 这将返回目录中的所有文件,并按最后修改日期倒序排序。 ### 回答2: 要获取最新的文件,可以使用以下步骤: 首先,需要引入`java.nio.file`和`java.nio.file.attribute`包,以便使用与文件相关的类和方法。 接下来,创建一个`List<java.nio.file.Path>`类型的变量,用于存储文件路径。 然后,使用`java.nio.file.Paths`类的`get`方法,传入文件路径作为参数,获取一个`java.nio.file.Path`对象,并将其添加到上述列表中。 接着,使用`java.nio.file.Files`类的`newest`方法,传入文件路径列表作为参数,获取最新的文件的`java.nio.file.Path`对象。 最后,将得到的最新文件的路径打印出来或做其他相应的处理。 以下是通过Java代码实现上述步骤的示例: ```java import java.nio.file.*; import java.nio.file.attribute.*; import java.io.IOException; import java.util.List; import java.util.Comparator; public class Main { public static void main(String[] args) { List<Path> filePaths = new ArrayList<>(); // 创建存储文件路径的列表 // 添加文件路径到列表中 filePaths.add(Paths.get("path/to/file1.txt")); filePaths.add(Paths.get("path/to/file2.txt")); filePaths.add(Paths.get("path/to/file3.txt")); try { // 获取最新的文件 Path newestFile = Files.newest(filePaths, new Comparator<Path>() { // 自定义比较器用于比较文件的最后修改时间 @Override public int compare(Path file1, Path file2) { try { FileTime fileTime1 = Files.getLastModifiedTime(file1); FileTime fileTime2 = Files.getLastModifiedTime(file2); return fileTime1.compareTo(fileTime2); } catch (IOException e) { e.printStackTrace(); } return 0; } }); System.out.println("最新的文件是:" + newestFile.toString()); } catch (IOException e) { e.printStackTrace(); } } } ``` 以上代码中,根据提供的文件路径列表,通过比较文件的最后修改时间,找到最新的文件并打印出来。请注意,需要替换示例代码中的文件路径,以便适应实际情况。 ### 回答3: 要获取最新的文件,可以使用以下方法: 1. 首先,我们需要获取文件所在的目录。假设文件所在的目录是 "/path/to/directory"。 2. 使用 java.nio.file 包提供的类和方法,我们可以通过以下方式获取文件夹下的所有文件: ``` Path directoryPath = Paths.get("/path/to/directory"); try (Stream<Path> fileStream = Files.list(directoryPath)) { List<Path> fileList = fileStream.collect(Collectors.toList()); } ``` 这样,我们就能够得到一个包含该目录下所有文件的列表 fileList。 3. 然后,我们可以通过对文件的修改时间进行比较,来找到最新的文件。可以使用 java.nio.file.attribute 包提供的类和方法来获取和比较文件的属性。 ``` Path newestFile = null; FileTime newestFileTime = null; for (Path file : fileList) { FileTime fileTime = Files.getLastModifiedTime(file); if (newestFileTime == null || fileTime.compareTo(newestFileTime) > 0) { newestFile = file; newestFileTime = fileTime; } } ``` 通过以上代码,我们可以找到最新的文件 newestFile,而 newestFileTime 表示该文件的最后一次修改时间。 总结起来,要获取最新的文件,我们可以通过以下步骤来完成: 1. 获取文件所在目录的路径。 2. 使用 Files.list() 方法获取该目录下的所有文件。 3. 遍历文件列表,通过 getLastModifiedTime() 方法获取文件的最后一次修改时间,并比较找到最新的文件。 以上是用 java List<java.nio.file.Path> 获取最新文件的方法。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值