0

I am very new to Java 8 features like streams, filters and stuff and the tell the truth, I haven't been writing in Java for more than a year. Here is my problem if someone could give a suggestion .

@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.

}

The main idea is that I want return all enabled agents. gerConfigState() returns an enum (__ConfigState). not sure If am doing this correctly.

  • 1
    Why fetch all of them from database and then filter the enabled ones in the memory? It's not a good Idea. Just fetch the enabled ones from database using a Criteria (where clause). – STaefi May 11 '16 at 08:14

3 Answers3

1

Use the collect-metod of the Stream. Also, your filter looks a bit strange, since the variable a is an object of class Agent.

So perhaps something like this:

agents.stream()
      .filter(a -> a.getConfigState() == Enabled)
      .collect(Collectors.toList());

Then again, like the comment states, you might just be better off filtering this with a query.

Tobb
  • 11,850
  • 6
  • 52
  • 77
  • I guess it looks strange. what about the return? I've tried your code (and other stuff too) but it mostly complains "Can not convert from List to Stream – Boris Petrov May 11 '16 at 08:22
  • I've tried this yesterday. the result was something like that I can not compare enum state. getConfigState() returns enum state not a boolean – Boris Petrov May 11 '16 at 08:30
  • Where does it complain about this? Which line? If getConfigState() return an enum constant, you should be able to compare it with another enum constant. – Tobb May 11 '16 at 09:04
1

Your filter condition is not correct (I assume getConfigState() returns an enum). You can use something like below:

Stream<Agent> streamAgent = agents.stream().filter(a-> a.getConfigState() == Enabled);    
return streamAgent.collect(Collectors.toList()); 
Vijay
  • 542
  • 4
  • 15
  • This is how I do it and it doesn't complain: Stream result = agents.stream() . filter(a-> a.getConfigState().equals("Enabled")); I am going to write a junit test to chek int. One more thing... there is need of cast to at the return. – Boris Petrov May 11 '16 at 08:35
  • You can use equals() or == to compare enum. The pros and cons discussed here already. http://stackoverflow.com/questions/1750435/comparing-java-enum-members-or-equals. I would prefer == as it safer. – Vijay May 11 '16 at 08:45
  • Also you are comparing the enum string rather than the enum itself. You should use a.getConfigState().equals(Enabled), passing in the actual enum, not the String. – Vijay May 11 '16 at 08:47
  • this ius the way I do it now: Stream result = repository.all() .stream() .filter(a-> a.getConfigState() .equals(ConfigState.Enabled)); return (ArrayList) result.collect(Collectors.toList()); – Boris Petrov May 11 '16 at 11:10
0

Thanks for the help. This is the final version:

@Override
public List<Agent> getAllEnabledAgents() throws Exception {
      return repository.all()
            .stream()
            .filter(a-> a.getConfigState() == ConfigState.Enabled)
            .collect(Collectors.toList());
}