[英]What is the fastest way to get the elements of a collection?
I have a List<Pair<String, String>>
into which I want to copy the data from a Collection
. 我有一个
List<Pair<String, String>>
,我要将Collection
的数据复制到其中。
What is the best way to read the collection and add items to list? 读取收藏集并将项目添加到列表的最佳方法是什么?
List<Pair<String, String>> identityMemoPairs = new LinkedList<Pair<String, String>>();
Collection result = handler.getResult();
while(result.iterator().hasNext()){
IdentityM im =(IdentityM) result.iterator().next();
identityMemoPairs.add(Pair.of(im.identity,im.memo));
}
Your code is wrong, because you create a new iterator at each iteration of the while loop (actually you create two of them). 您的代码是错误的,因为您在while循环的每次迭代中创建了一个新的迭代器(实际上您创建了其中的两个)。 Each fresh iterator will point to the beginning of the
result
collection. 每个新的迭代器都将指向
result
集合的开始。 Thus you have created an infinite loop. 这样就创建了一个无限循环。
To fix this, call result.iterator()
only once and store the result in a variable. 要解决此问题,只调用一次
result.iterator()
并将结果存储在变量中。
But even better (better to read, less error-prone) would be the for-each loop, which is (almost always) the preferred variant to iterate over collections: 但是,更好的方式(更好地阅读,更不易出错)将是for-each循环,它(几乎总是)是遍历集合的首选变体:
for (IdentityM im : (Collection<IdentityM>)result) {
identityMemoPairs.add(Pair.of(im.identity,im.memo));
}
The compiler will automatically transform this into code using the iterator, so no performance difference. 编译器将使用迭代器将其自动转换为代码,因此没有性能差异。 Generally, performance is anyway not a problem when iterating over a collection, as long as you avoid a few bad things like calling
get(i)
on a LinkedList
. 通常,只要避免一些不好的事情,例如在
LinkedList
上调用get(i)
对集合进行迭代时,性能始终不是问题。
Note that the compiler will give you a warning here, which has nothing to do with the iteration, but with the use of the raw type Collection
(instead of Collection<IdentityM>
). 请注意,编译器将在此处向您发出警告,该警告与迭代无关,但与原始类型
Collection
(而不是Collection<IdentityM>
)的使用有关。 Please check if handler.getResult()
actually returns a Collection<IdentityM>
and change the type of the result
variable to this if possible. 请检查
handler.getResult()
实际上返回Collection<IdentityM>
并在可能的情况下将result
变量的类型更改为此。
Another question is whether you really need that list to be a list of pairs. 另一个问题是您是否真的需要将该列表作为成对的列表。 Usually using plain pair classes is not recommended, because their name does not show the meaning of the object they represent.
通常不建议使用纯对类,因为它们的名称不显示其表示的对象的含义。 For example, it is better to use a class
PersonName
which has fields for first and last name instead of a Pair<String, String>
. 例如,最好使用
PersonName
类,该类具有用于名字和姓氏的字段,而不要使用Pair<String, String>
。 Why can't you just use a List<IdentityM>
? 为什么不能只使用
List<IdentityM>
? And if you can use this, are you sure you can't use a Collection<IdentityM>
? 并且,如果可以使用它,确定不能使用
Collection<IdentityM>
吗? ( List
and Collection
are often exchangeable.) Then you could avoid the copying altogether. (
List
和Collection
通常可以互换。)这样就可以避免完全复制。
Your code is imho good as it is. 您的代码确实很棒。 But you could probabbly make the
handler
return a collection of Pairs directly so you could call identityMemoPairs.addAll()
instead of iterating the collection yourself. 但是您可以使
handler
直接返回一个Pairs集合,因此您可以调用identityMemoPairs.addAll()
而不是自己迭代该集合。 But that only makes it "prettier" it doesn't give more performance. 但这只会使其“更漂亮”,而不会提供更多性能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.