简体   繁体   English

用于队列和堆栈的Java LinkedList方法

[英]Java LinkedList methods for Queue and Stack

if you look at the LinkedList methods of Java, it does offer operations for Queue, Stack, Deque. 如果您查看Java的LinkedList方法,它将为Queue,Stack和Deque提供操作。

And I am aware that you can implement Queue, Stack or Deque with a LinkedList. 而且我知道您可以使用LinkedList实现Queue,Stack或Deque。 If you look at C# implementation though, Queue and Stack uses arrays. 但是,如果您看一下C#实现,则Queue和Stack使用数组。

My Curiosity is, why they offer a push(T e) method for a linked list? 我的好奇心是,为什么他们为链接列表提供push(T e)方法?

Why Queue and Stack are not separate classes, just like C#. 为什么Queue和Stack不是像C#一样是单独的类。

Below is the code for push and pop which is duhhh. 以下是duhhh的push和pop代码。 But why? 但为什么?

public void push(Object obj)
{
    addFirst(obj);
}

public Object pop()
{
    return removeFirst();
}

If you look at HashMap, or HashSet, it uses array internally, and there is LinkedHashSet and map correspondingly to keep ordering. 如果您查看HashMap或HashSet,它在内部使用数组,并且存在LinkedHashSet和对应的map以保持顺序。

This is not really confusing, but it doesnt make sense really. 这并不是很令人困惑,但实际上没有任何意义。

Why java has such implementation? 为什么Java有这样的实现?

Focus on data structure implementation: 专注于数据结构的实现:

Linked list is efficient for frequent add and remove. 链接列表对于频繁添加和删除非常有效。 (as Queue and Stack usually do, and iteration operation is rare). (就像Queue和Stack通常一样,并且很少执行迭代操作)。 Array is not, it needs array-copy operation which is time consuming . 数组不是,它需要耗时的数组复制操作。

A double-linked list such as Java's LinkedList is a rather flexible data structure, so it makes sense to implement several data structures using it as a base. 诸如Java的LinkedList之类的双链表是一种相当灵活的数据结构,因此以它为基础来实现多个数据结构是有意义的。 So if you want to view it as a Queue, you'd say something like this (I'm omitting type parameters): 因此,如果您想将其视为一个队列,则可以这样说(我省略了类型参数):

Queue q = new LinkedList();

If you want to use it as a stack, you would declare it like this: 如果要将其用作堆栈,则可以这样声明:

Deque s = new LinkedList();

And so on. 等等。 It all boils down to code reutilization, after all, why implement several different classes for similar functionality, when a single class (LinkedList in this case) suffices? 归根结底,归结为代码重用,毕竟,当单个类(在这种情况下为LinkedList)足够用时,为什么要为类似的功能实现几个不同的类?

LinkedList implements Queue interface because you might want to use the linked list as a Queue in some places. LinkedList实现了Queue接口,因为您可能希望在某些地方将链接列表用作Queue。 What this means is that a method which takes a queue as input param can also process linked lists. 这意味着以队列为输入参数的方法也可以处理链表。 The following works 以下作品

List<String> linkedList = new LinkedList<String>();
linkedList.add("element1");
linkedList.add("element2");

Queue<String> q = (Queue<String>)linkedList; 
q.poll(); //removes and returns element1 from the linkedList

Java has a separate class called java.util.Stack which extends from vector which in turn is a array based implementation. Java有一个单独的类java.util.Stack ,它从vector扩展而来,而vector又是基于数组的实现。 But this is a thread safe version. 但这是线程安全的版本。 If you don't worry about thread safety, then you can use ArrayDeque as a stack. 如果您不担心线程安全,则可以将ArrayDeque用作堆栈。

Basic OOD: while perhaps a fuzzy line, Queue, Stack, and Deque roughly describe operations you can perform on a collection and hence deserve to be interfaces. 基本OOD:虽然模糊线,Queue,Stack和Deque粗略地描述了您可以对集合执行的操作,因此值得作为接口。 LinkedList describes the implementation and performance characteristics of an interface and hence deserves to be a class. LinkedList描述了接口的实现和性能特征,因此值得一类。 That implementation could be (is) exposed as multiple interfaces. 该实现可以作为多个接口公开。 To me, the real question is, "Why is Stack a class?" 对我来说,真正的问题是:“为什么要给Stack上课?”

Queue is an interface, and has other implementations besides LinkedList. 队列是一个接口,除LinkedList外还有其他实现。 There's also a Stack class. 还有一个Stack类。

Ultimately, it just seems like an arbitrary decision, and the underlying implementation doesn't really matter that much (IMO). 最终,这似乎是一个任意决定,而底层实现实际上并没有那么重要(IMO)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM