[英]Cost of inserting element at 0th position of LinkedHashSet?
I'm using LinkedHashSet. 我正在使用LinkedHashSet。 I want to insert items at the 0th position, like: 我想在第0个位置插入项目,例如:
Set<String> set = new LinkedHashSet<String>();
for (int i = 0; i < n; i++) {
set.add(0, "blah" + i);
}
I'm not sure how linked hash set is implemented, is inserting going to physically move all addresses of current items, or is it the same cost as inserting as in a linked-list implementation? 我不确定链接哈希集是如何实现的,插入是否会物理移动当前项目的所有地址,或者与插入链表实现中的插入成本相同?
Thank you 谢谢
------ Edit --------------- ------编辑---------------
Complete mess up by me, was referencing ArrayList docs. 我完全搞砸了,是在引用ArrayList文档。 The Set interface has no add(index, object) method. Set接口没有add(index,object)方法。 Is there a way to iterate over the set backwards then? 那么有没有办法向后迭代集合呢? Right now to iterate I'm doing: 现在要迭代,我正在做:
for (String it : set) {
}
can we do that in reverse? 我们可以反向做吗?
Thanks 谢谢
Sets are, by definition, independent of order. 根据定义,集独立于顺序。 Thus, Set doesn't have add(int , Object) method available. 因此,Set没有可用的add(int,Object)方法。
This is also true of LinkedHashSet http://download.oracle.com/javase/6/docs/api/java/util/LinkedHashSet.html LinkedHashSet http://download.oracle.com/javase/6/docs/api/java/util/LinkedHashSet.html也是这样
LinkedHashSet maintains insertion order and thus all elements are added at the end of the linked list. LinkedHashSet保持插入顺序,因此所有元素都添加到链接列表的末尾。 This is achieved using the LinkedHashMap. 这是使用LinkedHashMap实现的。 You can have a look at the method linkEntry in LinkedHashMap http://www.docjar.com/html/api/java/util/LinkedHashMap.java.html 您可以在LinkedHashMap http://www.docjar.com/html/api/java/util/LinkedHashMap.java.html中查看方法linkEntry
Edit: in response to edited question 编辑:回应已编辑的问题
There is no API method available to do this. 没有可用的API方法来执行此操作。 But you can do the following 但是您可以执行以下操作
ArrayList(Set)
使用新的ArrayList(Set)
添加到列表中 Collections.reverse(List)
使用Collections.reverse(List)
从LinkedHashMap的源代码(支持LinkedHashSet的源代码-参见http://www.docjar.com/html/api/java/util/LinkedHashMap.java.html )来看,插入很便宜,就像在链接列表中一样。
You can't add elements to the front of a LinkedHashSet
... it has no method such as add(int, Object)
nor any other methods that make use of the concept of an "index" in the set (that's a List
concept). 您不能在LinkedHashSet
的前面添加元素...它没有诸如add(int, Object)
方法,也没有任何其他使用集合中“索引”概念的方法(即List
概念) )。 It only provides consistent iteration order, based on the order in which elements were inserted. 它仅根据插入元素的顺序提供一致的迭代顺序。 The most recently inserted element that was not already in the set will be the last element when you iterate over it. 遍历该集合时,最后一个未插入的元素将是集合中的最后一个元素。
And the Javadoc for LinkedHashSet
explicitly states: LinkedHashSet
的Javadoc明确指出:
Like HashSet, it provides constant-time performance for the basic operations (add, contains and remove), assuming the hash function disperses elements properly among the buckets. 像HashSet一样,它为基本操作(添加,包含和删除)提供恒定时间的性能,假设哈希函数将元素正确地分布在存储桶中。
Edit : There is not any way to iterate over a LinkedHashSet
in reverse short of something like copying it to a List
and iterating over that in reverse. 编辑 :没有任何方法可以像反向将LinkedHashSet
反向复制一样,将其复制到List
并对其进行反向迭代。 Using Guava you could do that like: 使用番石榴,您可以这样做:
for (String s : Lists.reverse(ImmutableList.copyOf(set))) { ... }
Note that while creating the ImmutableList
does require iterating over each element of the original set, the reverse
method simply provides a reverse view and doesn't iterate at all itself. 请注意,虽然创建ImmutableList
确实需要迭代原始集合的每个元素,但是reverse
方法只是提供了一个反向视图,而本身并没有完全迭代。
To answer your latest question, there is no reverse iterator feature available from LinkedHashSet
, even though internally the implementation uses a doubly linked list. 为了回答您的最新问题, LinkedHashSet
没有可用的反向迭代器功能,即使该实现在内部使用双重链接列表也是如此。
There is an open Request For Enhancement about this: 对此有一个开放的增强请求:
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4848853 http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4848853
Mark Peters links to functionality available in guava, though their reverse list actually generates a reverse list. 马克·彼得斯(Mark Peters)链接到番石榴中可用的功能,尽管他们的反向列表实际上会生成反向列表。
As already mentioned, LinkedHashSet is build on LinkedHashMap, which is built on HashMap :) Javadocs says that it takes constant time to add an element into HashMap, assuming that your hash function is implemented properly. 如前所述,LinkedHashSet建立在LinkedHashMap之上,而LinkedHashMap则建立在HashMap之上:) Javadocs说,假设您的哈希函数已正确实现,则向HashMap中添加元素需要花费固定的时间。 If your hash function is not implemented well, it may take up to O(n). 如果您的散列函数实现不当,则可能需要O(n)。 Iteration backwards in not supported at this moment. 目前不支持向后迭代。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.