我对Java 8的特性非常陌生,比如流、过滤器和其他东西,说实话,我已经有一年多没有用Java写东西了。如果有人能给我一个建议,这就是我的问题。
@Override
public ArrayList<Agent> getAllEnabledAgents() throws Exception {
ArrayList<Agent> agents = repository.all(); //redis repository
Stream<Agent> result = agents.stream().filter(a-> a.equals(a.getConfigState().Enabled)); //enum
return result; //I dont know how to return result or whether I am using stream correctly.
}主要的想法是我想返回所有已启用的代理。gerConfigState()返回一个枚举(__ConfigState)。不确定我这样做是否正确。
发布于 2016-05-11 16:14:24
使用Stream的collect-metod。此外,由于变量a是Agent类的对象,因此您的过滤器看起来有点奇怪。
所以可能是这样的:
agents.stream()
.filter(a -> a.getConfigState() == Enabled)
.collect(Collectors.toList());再说一次,就像注释状态一样,你最好用一个查询来过滤。
发布于 2016-05-11 16:26:34
您的过滤条件不正确(我假设getConfigState()返回一个枚举)。您可以使用类似于下面的内容:
Stream<Agent> streamAgent = agents.stream().filter(a-> a.getConfigState() == Enabled);
return streamAgent.collect(Collectors.toList()); 发布于 2016-05-11 19:13:41
谢谢你的帮助。这是最终版本:
@Override
public List<Agent> getAllEnabledAgents() throws Exception {
return repository.all()
.stream()
.filter(a-> a.getConfigState() == ConfigState.Enabled)
.collect(Collectors.toList());
}https://stackoverflow.com/questions/37156659
复制相似问题