To properly add IOException (to RuntimeException) handling code, your method will look like this:
Stream<Account> s = accounts.values().stream();
s = s.filter(a -> { try { return a.isActive(); }
catch (IOException e) { throw new RuntimeException(e); }});
Stream<String> ss = s.map(a -> { try { return a.getNumber() }
catch (IOException e) { throw new RuntimeException(e); }});
return ss.collect(Collectors.toSet());
The problem now is that the IOException
will have to be captured as a RuntimeException
and converted back to an IOException
-- and that will add even more code to the above method.
Why use Stream
when it can be done just like this -- and the method throws IOException
so no extra code is needed for that too:
Set<String> set = new HashSet<>();
for(Account a: accounts.values()){
if(a.isActive()){
set.add(a.getNumber());
}
}
return set;