简体   繁体   English

在LinkedHashSet的第0个位置插入元素的成本?

[英]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 但是您可以执行以下操作

  1. Add Set to a List using new ArrayList(Set) 使用新的ArrayList(Set)添加到列表中
  2. Use Collections.reverse(List) 使用Collections.reverse(List)
  3. Iterate this 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.

相关问题 第i个元素的值更改影响ArrayList第0个元素的值 - value change in ith element influences value in the 0th element for ArrayList 从Arraylist中删除第0个元素时出现IndexOutOfBoundsException - IndexOutOfBoundsException when removing 0th element from Arraylist 如何使用自定义方法在第 0 个位置的链表中插入节点? - How do I insert a node in linked list at 0th position using a custom method? 是否正确,如果冒泡排序的第一遍将最小的气泡放在第 0 位? - Is it right, if first pass of Bubble Sort put smallest bubble in 0th position? 当数组中的第n个元素更新时,自定义适配器Android Studio中的第0个元素也会更新。 如何解决这个问题 - When nth element in array is updated, 0th element is also updated inside custom adapter Android studio. How to resolve this Java - 第0个局部变量何时不是&#39;this&#39;? - Java - When is the 0th local variable not 'this'? Spring Boot Data JPA 中的自加入 - 仅显示第一个子节点(第 0 个元素)而不是所有子节点 - Self Join in Spring Boot Data JPA - Showing only the first child node (0th element) not all children nodes 在数组中的任意 position 处插入一个元素 - Inserting an element at any position in array 如何替换LinkedHashSet中的特定元素? - How to replace a specific element in a LinkedHashSet? 更新 LinkedHashSet 的元素,它是一个集合 - Update element of LinkedHashSet which is a collection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM