Yes, that should work for Strings, but if you are worried about duplicates use a Set
. This collection prevents duplicates without you having to do anything. A HashSet
is OK to use, but it is unordered so if you want to preserve insertion order you use a LinkedHashSet
.