#1795 Use ArrayList instead of HashSet (cherry-picked from 3.x branch)

This fix didn't get merged into master after being incorporated into the 3.1.6 release.