Can Spring LdapTemplate stream results?

Viewed 232

I am trying to query an active directory for ALL person objects. There are over 700,000 results which are way too many to read into a List. My current solution is using paging to get all of the results but I would much rather use the Java-8 Stream-API to get them instead. Unfortunately, I have not found any methods on LdapTemplate that return a Stream object.

I know that this can return all of the results:

public List<LdapPerson> findAll() {
        return ldapTemplate.search("","(objectClass=person)", mapper);
    }

but what I'm looking for is something that can return:

public Stream<LdapPerson> findAll(){
   return ldaptemplate.?????????;
}

Can anyone point me in the right direction?

1 Answers

No, the LdapTemplate doesn't provide a method that directly returns a Stream<T> from a searched elements. However, since the method, LdapTemplate::search returns a List<T>, then you can use a characteristics of any Collection<T> that is convertible to Stream<T> using a simple call of collection.stream() method:

public Stream<LdapPerson> findAll() {
    return ldapTemplate.search("", "(objectClass=person)", mapper).stream();
}
Related