Java JHipster生成的代码-LinkedList vs List
我想知道为什么选择在AccountResource.getAccount的生成代码中使用LinkedListJava JHipster生成的代码-LinkedList vs List,java,jhipster,Java,Jhipster,我想知道为什么选择在AccountResource.getAccount的生成代码中使用LinkedList /** * GET /account -> get the current user. */ @RequestMapping(value = "/account", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) @Timed public Res
/**
* GET /account -> get the current user.
*/
@RequestMapping(value = "/account",
method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
@Timed
public ResponseEntity<UserDTO> getAccount() {
return Optional.ofNullable(userService.getUserWithAuthorities())
.map(user -> {
return new ResponseEntity<>(
new UserDTO(
user.getLogin(),
null,
user.getFirstName(),
user.getLastName(),
user.getEmail(),
user.getLangKey(),
user.getAuthorities().stream().map(Authority::getName).collect(Collectors.toCollection(LinkedList::new))),
HttpStatus.OK);
})
.orElse(new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR));
}
对不起,我遗漏了一些明显的东西。我刚刚过渡到Java 8,我的Lambda知识还远未完成。这是开源的,所以让我们看看源代码,做一个责备,我们发现:。显然,这样做只是为了删除IDEA中的警告。使用Collectors.toList,您不能指定实现类 javadoc: 返回一个收集器,该收集器将输入元素累加到新的 列表对于类型、可变性、, 可序列化性,或返回列表的线程安全性;如果更多 需要对返回的列表进行控制,请使用 收集供应商
LinkedList非常适合只包含几个项目的短列表。对于那些你可能只有一件物品的权威机构来说,这似乎是足够的。例如,一个ArrayList将分配一个数组。忘了提到我将新的ResponseEntity包装为一个对象{}。在此之前,Intellij一直在抱怨:无法解析方法“collectjava.util.stream.collectorhanks JP”。有趣的是,我的代码和作者的代码一样。唯一的区别是我必须用一个物体来包裹Intellij。它似乎工作得很好:谢谢你,乔普。也许是为了线的安全。虽然我希望在设计时添加角色,并且角色是固定的。考虑到这是一种get方法,我认为肮脏的阅读是可以的。我只是想知道是否还有更多的事情。。。我遗漏了一个基本点。
...
user.getAuthorities().stream().map(Authority::getName).collect(Collectors.toList())),
...