Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
在编写 junit 测试用例时,以下哪一项最适合用于空列表?
List<String> list = new ArrayList<String>();
或者
Collections.<String> emptyList();
?
只要有可能,我更喜欢Collections.emptyList(),ArrayList因为这样可以确保主代码不会尝试修改给定的列表,这在大多数情况下是正确的行为。
Collections.emptyList()
ArrayList
在实践中,我使用 Guava's ImmutableList,因为它不仅涵盖空列表,还涵盖非空列表。
ImmutableList