Java数据结构,保留顺序,不允许重复,并允许在开始和结束时删除和插入
是否有一种 Java 数据结构:
- 不允许重复
- 保留插入顺序
- 允许在集合的开头或末尾删除和插入
有 LinkedHashSet
,但它只允许 remove(object)
、add(object)
按照Set
。
Is there a Java data structure that:
- does not allow duplicates
- retains insertion order
- allows removal and insertion at either the start or end of the collection
There is LinkedHashSet
, but it only allows remove(object)
, add(object)
as per Set
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
LinkedHashSet 将允许删除第一个元素,只需执行
LinkedHashSet will allow removal of the first element, just do
从 Java 21 开始,
LinkedHashSet
满足规定的要求。LinkedHashSet
已更新为具有addFirst
, <代码>addLast,removeFirst
和removeLast
方法。当与LinkedHashSet
先前存在的功能相结合时,这可以满足规定的要求。As of Java 21,
LinkedHashSet
meets the stated requirements.LinkedHashSet
was updated to haveaddFirst
,addLast
,removeFirst
, andremoveLast
methods. This, when combined with the previously existing capabilities ofLinkedHashSet
, meets the stated requirements.如果您愿意为了不重复而放弃一点性能,则可以随时扩展
ArrayDeque
并重写所有插入到集合中的方法以查看该元素是否已存在。If you're willing to give up a little performance for no duplicates, you can always extend
ArrayDeque
and override all the methods that insert into the collection to see if the element already exists.