[英]Can this Java code be made more efficient
I have Java code that applies a function to every element of a HashSet. 我有将功能应用于HashSet的每个元素的Java代码。 Each element has to be (1) removed from the HashSet,(2)processed, which returns a result of the same type (and sometimes even the same object instance), and sometimes multiple instances of this type in a collection; 每个元素都必须(1)从HashSet中删除,然后进行处理(2),这将返回相同类型的结果(有时甚至返回相同的对象实例),并且有时在集合中返回该类型的多个实例; (3)the result is replaced in a HashSet. (3)将结果替换为HashSet。
I delete each element from the original HashSet, processing it's elements until its empty. 我从原始HashSet中删除每个元素,处理它的元素直到其为空。 I place each new instance returned by processing the object in a new HashSet. 我将通过处理对象返回的每个新实例放置在新的HashSet中。 When I'm done I discard the old HashSet and continue with the new one. 完成后,我将丢弃旧的HashSet并继续使用新的HashSet。 I thought I had to do this because otherwise, I risk an infinite loop as I iterate over every element of HashSet while also adding elements to it. 我以为我必须这样做,因为否则,当我遍历HashSet的每个元素并向其中添加元素时,我将面临无限循环的风险。 The code kinda looks like this. 代码有点像这样。 Fct addToHashSet adds 1 or more elements to newSet. Fct addToHashSet将1个或多个元素添加到newSet中。
newSet= new HashSet< myObjectType >();
for (myObjectType s : origSet){
addToHashSet(newSet, process(s,message));
}
return newSet;
My questions are: 我的问题是:
1) Am I being inefficient by constantly creating and deleting HashSets (this processing is called a lot)? 1)我不断地创建和删除HashSets效率低下吗?
2) If so, is there a better, in place, manner to process every element once, (I don,t want to process elements that I just added) without creating a new HashSet? 2)如果是这样,有没有一种更好的方法来处理每个元素一次(我不想处理刚刚添加的元素)而不创建新的HashSet?
3) If the answer is no, can it be dome for cases where each element of the Hashset is replaced with a single instance? 3)如果答案是否定的,那么在哈希集的每个元素都被单个实例替换的情况下,它可以是圆顶吗? That code looks like that: 该代码如下所示:
newSet= new HashSet< myObjectType >();
for (myObjectType s : origSet){
newSet .add(process(s,message));
}
return newSet;
If you really need the properties of a Set
, and if your process returns new instances, as opposed to modifying the instances in the set, then your current solution is fine. 如果您确实需要Set
的属性,并且您的过程返回了新实例(而不是修改set中的实例),那么当前的解决方案就可以了。
If the process modifies the instances instead of returning new instances, then you can use the forEach
method instead: 如果该过程修改了实例而不是返回新实例,则可以改用forEach
方法:
origSet.forEach(item -> process(item));
If you don't need the properties of a Set
, then you could use a Queue<>
instead: 如果不需要Set
的属性,则可以使用Queue<>
代替:
int size = queue.size();
for (int i = 0; i < size; i++) {
queue.add(process(queue.poll()));
}
At the end of this loop, the original elements of the queue
will be all gone, it will contain the new elements returned by process
. 在此循环结束时, queue
的原始元素将全部消失,它将包含由process
返回的新元素。
And if you really need a new set replacing the old one, then you can use a more idiomatic solution with streams (as @Simon pointed out in a comment): 而且,如果您确实需要一个新集合来代替旧集合,则可以对流使用更惯用的解决方案(如@Simon在评论中指出的那样):
newSet = origSet.stream().map(s -> process(s, message)).collect(Collectors.toSet());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.