简体   繁体   English

获取集合元素的最快方法是什么?

[英]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. ListCollection通常可以互换。)这样就可以避免完全复制。

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.

相关问题 在Java中获取k个最小(或最大)数组元素的最快方法是什么? - What is the fastest way to get k smallest (or largest) elements of array in Java? 在集合集合中添加元素的最快方法 - The fastest way to add elements in set collection 更改 ArrayList 中元素顺序的最快方法是什么? - What is the fastest way to change the order of elements in an ArrayList? 在Java中比较两组以获取一组而不是另一组中的元素的最快方法是什么 - What is the fastest way to compare two sets in Java to get the elements that are in one set and not the other 在Java中,获取系统时间最快的方法是什么? - In Java, what is the fastest way to get the system time? 最快的方法来知道字符集合中是否存在字母 - What is the fastest way to know if a letter exists in a Character collection 从Firestore获取数据的最快方法是什么? - What is the fastest way to get data from Firestore? 获取第n个nextInt值的最快方法是什么? - what is the fastest way to get the nth nextInt value? 将Java数组中任意范围的元素设置为null的最快方法是什么? - What is the fastest way to set an arbitrary range of elements in a Java array to null? 仅旋转阵列中某些元素的最快方法是什么? - What's the fastest way to only rotate certain elements in an array?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM