I already have a List<T>
, which may or may not contain duplicates. I am going to build a Set<String>
based on some result of computation using them, because two different T
s can produce the same String
under some circumstances.
Some of the elements in the List<T>
have undesirable attributes, so I need to filter them out. I filter like this:
List<T> myList = myCoolListGetter();
Iterator<T> it = myList.iterator();
T curr;
while (it.hasNext()) {
curr = it.next();
if (curr.shouldNotBeInResult()) {
myList.remove(curr);
}
}
I then pass this List<T>
to another method which performs those computations I mentioned, adding them to a Set<String>
.
I'm thinking perhaps I could save some time by inserting the elements which should be in the result rather into a Set<T>
rather than removing them from the List<T>
. Since I am already iterating over the List<T>
here, and have to construct a Set<String>
later anyway, would I actually be saving time by doing that?