Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Is there any (well implemented) intrusive double linked list class(es) available for Java? Or should I do my own? Boost has it for C++: http://beta.boost.org/doc/libs/1_40_0/doc/html/boost/intrusive/list.html. Intrusive list is a container having (in this case) next and prev pointers within element, so typical list operations like replace and remove can be directly targeted into elementary cla...
I'm migrating a piece of code to make use of generics. One argument for doing so is that the for loop is much cleaner than keeping track of indexes, or using an explicit iterator. In about half the cases, the list (an ArrayList) is being iterated in reverse order by using an index. Can someone suggest a cleaner way of doing this (since I dislike the indexed for loop when working with collecti...
I recently had a discussion with a collegue why the List interface in Java doesn't have a head() and tail() method. In order to implement such a functionality would have to write a wrapper that looked something like this: public E head() { if (underlyingList == null || underlyingList.isEmpty()) return null; return underlyingList.get(0); } public E tail() { if (underlyingList == null |...
LinkedList has similar descriptions for the element() method and the getFirst() method (strangely - not the same words). Deque clearly states that the two methods are the same in terms of the return value and the exception. My question is - why have 2 identical methods? Is it for backward compatibility? Is one approach more efficient than the other?
Why aren't ArrayLists generally implemented to be double-ended, which would support fast amortized insertion in the front as well as the back? Is there ever a disadvantage to using the latter over the former? (I'm not talking just about Java -- I haven't seen double-ended array lists being the default in any other language, but Java was just a good example here.) *Edit: I originally called...
I created a program that contains linked lists that are passed various methods. While this works just fine in Java... a style checker program we have to use doesn't like it It says: Declaring variables, return values or parameters of type 'LinkedList' is not allowed. If I declare them as simply List then I don't have access to the methods I want. What should I do?
This code: class RawStringIterator { java.util.Stack<State> stateStack = new java.util.Stack<State>(); RawStringIterator(RawStringIterator i) { stateStack = (java.util.Stack<State>) i.stateStack.clone(); } /* ... */ } gives me this warning: Type safety: Unchecked cast from Object to Stack<Utils.OperatorTree.RawStringIterator...
I want to push some int to a priorityqueue but i can't! i used the queue.add() code but this code will return the sorted queue,please help,thank you!
1) My problem when i make remove from right or left program will be remove true but when i call diplay method the content wrong like this I insert 12 43 65 23 and when make remove from left program will remove 12 but when call display method show like this 12 43 65 and when make remove from right program will remove 23 but when call display method show like this 12 43 Why ?????? ...
Are there any good books/websites for learning about the low-level details on Java collections/structures? I'd like to find out the fastest way to implement a Last In, First Out (LIFO) structur similar to a stack, but im a little worried the stack seems too 'neat' and 'perfect' that it must have some disadvantages? Is it slower? would it be easier to take a non thread-safe structure and write ...
Hi this is my class and I want to sort my stack but it will throw an exception please help me thanks! public class jj { public static void main(String[] args){ Stack<Integer> s = new ImplimentingAStackUsingAnArrayOfAGivenSizeN(5); s.push(1); s.push(3); s.push(5); s.push(2); s.push(4); Collections.sort((List<Integer>) (s)); System.out.println(s); while (!s.isEmpty()) { System.o...
I'm looking for a data structure that is basically a bounded stack. If I declare that the stack can hold at most 3 items, and I push another item in, the oldest item is popped.
I need to create a FIFO queue. I was thinking in creating a LinkedList for that, because of it's native methods to remove and add. But my queue should have a fixed size, so how could I fix that size? Thanks in advance!
Hmm. I'm noticing Stack is a subclass of Vector, and I thought Vector and Hashtable were considered "old" datastructures because of their builtin synchronization even if you don't need it. (vs. List, Map, etc. which don't provide it for you) That and it's a class, not an interface. Is there a more modern, recommended alternative?
in C++ all I had to do was #include <queue> -> including queue<int> a; -> defining a.push(1); ->using but in java I found very difficult to use simple deque what should I do...? more specifically, How should I code to simply do the same steps as I did in C++; including, defining, using. even more specifically, I want to make a deque so that I can add any integer in th...
I know that I am missing something simple here. I have got this homework all done except for moving through my ArrayList. This is a undo feature of a calculator that I need to pull and remove a object from an ArrayList. Here is the method: public void actionPerformed(ActionEvent e) { Status state; state = new Status(operand1, operator, operand2, displayBox.getText()); ...
I've got to write a very short bit of code on a deque, however I'm not sure how to write the code for the methods, if someone could help me with one of the methods, (eg. a method to add an object to the from of the deque) then that would get me started. I'm sure I could manage the rest of the methods, just at the moment I'm pretty stumped. Many thanks.
  /*
   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   *
   * This code is free software; you can redistribute it and/or modify it
   * under the terms of the GNU General Public License version 2 only, as
   * published by the Free Software Foundation.  Sun designates this
   * particular file as subject to the "Classpath" exception as provided
   * by Sun in the LICENSE file that accompanied this code.
   *
  * This code is distributed in the hope that it will be useful, but WITHOUT
  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  * version 2 for more details (a copy is included in the LICENSE file that
  * accompanied this code).
  *
  * You should have received a copy of the GNU General Public License version
  * 2 along with this work; if not, write to the Free Software Foundation,
  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  *
  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  * CA 95054 USA or visit www.sun.com if you need additional information or
  * have any questions.
  */
 
 /*
  * This file is available under and governed by the GNU General Public
  * License version 2 only, as published by the Free Software Foundation.
  * However, the following notice accompanied the original version of this
  * file:
  *
  * Written by Doug Lea and Josh Bloch with assistance from members of
  * JCP JSR-166 Expert Group and released to the public domain, as explained
  * at http://creativecommons.org/licenses/publicdomain
  */
 
 package java.util;

A linear collection that supports element insertion and removal at both ends. The name deque is short for "double ended queue" and is usually pronounced "deck". Most Deque implementations place no fixed limits on the number of elements they may contain, but this interface supports capacity-restricted deques as well as those with no fixed size limit.

This interface defines methods to access the elements at both ends of the deque. Methods are provided to insert, remove, and examine the element. Each of these methods exists in two forms: one throws an exception if the operation fails, the other returns a special value (either null or false, depending on the operation). The latter form of the insert operation is designed specifically for use with capacity-restricted Deque implementations; in most implementations, insert operations cannot fail.

The twelve methods described above are summarized in the following table:

First Element (Head) Last Element (Tail)
Throws exceptionSpecial valueThrows exceptionSpecial value
InsertaddFirst(e)offerFirst(e)addLast(e)offerLast(e)
RemoveremoveFirst()pollFirst()removeLast()pollLast()
ExaminegetFirst()peekFirst()getLast()peekLast()

This interface extends the Queue interface. When a deque is used as a queue, FIFO (First-In-First-Out) behavior results. Elements are added at the end of the deque and removed from the beginning. The methods inherited from the Queue interface are precisely equivalent to Deque methods as indicated in the following table:

Queue Method Equivalent Deque Method
add(e)addLast(e)
offer(e)offerLast(e)
remove()removeFirst()
poll()pollFirst()
element()getFirst()
peek()peekFirst()

Deques can also be used as LIFO (Last-In-First-Out) stacks. This interface should be used in preference to the legacy Stack class. When a deque is used as a stack, elements are pushed and popped from the beginning of the deque. Stack methods are precisely equivalent to Deque methods as indicated in the table below:

Stack Method Equivalent Deque Method
push(e)addFirst(e)
pop()removeFirst()
peek()peekFirst()

Note that the peek method works equally well when a deque is used as a queue or a stack; in either case, elements are drawn from the beginning of the deque.

This interface provides two methods to remove interior elements, removeFirstOccurrence and removeLastOccurrence.

Unlike the List interface, this interface does not provide support for indexed access to elements.

While Deque implementations are not strictly required to prohibit the insertion of null elements, they are strongly encouraged to do so. Users of any Deque implementations that do allow null elements are strongly encouraged not to take advantage of the ability to insert nulls. This is so because null is used as a special return value by various methods to indicated that the deque is empty.

Deque implementations generally do not define element-based versions of the equals and hashCode methods, but instead inherit the identity-based versions from class Object.

This interface is a member of the Java Collections Framework.

Parameters:
<E> the type of elements held in this collection
Author(s):
Doug Lea
Josh Bloch
Since:
1.6
public interface Deque<E> extends Queue<E> {
    
Inserts the specified element at the front of this deque if it is possible to do so immediately without violating capacity restrictions. When using a capacity-restricted deque, it is generally preferable to use method offerFirst(java.lang.Object).

Parameters:
e the element to add
Throws:
java.lang.IllegalStateException if the element cannot be added at this time due to capacity restrictions
java.lang.ClassCastException if the class of the specified element prevents it from being added to this deque
java.lang.NullPointerException if the specified element is null and this deque does not permit null elements
java.lang.IllegalArgumentException if some property of the specified element prevents it from being added to this deque
    void addFirst(E e);

    
Inserts the specified element at the end of this deque if it is possible to do so immediately without violating capacity restrictions. When using a capacity-restricted deque, it is generally preferable to use method offerLast(java.lang.Object).

This method is equivalent to add(java.lang.Object).

Parameters:
e the element to add
Throws:
java.lang.IllegalStateException if the element cannot be added at this time due to capacity restrictions
java.lang.ClassCastException if the class of the specified element prevents it from being added to this deque
java.lang.NullPointerException if the specified element is null and this deque does not permit null elements
java.lang.IllegalArgumentException if some property of the specified element prevents it from being added to this deque
    void addLast(E e);

    
Inserts the specified element at the front of this deque unless it would violate capacity restrictions. When using a capacity-restricted deque, this method is generally preferable to the addFirst(java.lang.Object) method, which can fail to insert an element only by throwing an exception.

Parameters:
e the element to add
Returns:
true if the element was added to this deque, else false
Throws:
java.lang.ClassCastException if the class of the specified element prevents it from being added to this deque
java.lang.NullPointerException if the specified element is null and this deque does not permit null elements
java.lang.IllegalArgumentException if some property of the specified element prevents it from being added to this deque
    boolean offerFirst(E e);

    
Inserts the specified element at the end of this deque unless it would violate capacity restrictions. When using a capacity-restricted deque, this method is generally preferable to the addLast(java.lang.Object) method, which can fail to insert an element only by throwing an exception.

Parameters:
e the element to add
Returns:
true if the element was added to this deque, else false
Throws:
java.lang.ClassCastException if the class of the specified element prevents it from being added to this deque
java.lang.NullPointerException if the specified element is null and this deque does not permit null elements
java.lang.IllegalArgumentException if some property of the specified element prevents it from being added to this deque
    boolean offerLast(E e);

    
Retrieves and removes the first element of this deque. This method differs from pollFirst only in that it throws an exception if this deque is empty.

Returns:
the head of this deque
Throws:
NoSuchElementException if this deque is empty
    E removeFirst();

    
Retrieves and removes the last element of this deque. This method differs from pollLast only in that it throws an exception if this deque is empty.

Returns:
the tail of this deque
Throws:
NoSuchElementException if this deque is empty
    E removeLast();

    
Retrieves and removes the first element of this deque, or returns null if this deque is empty.

Returns:
the head of this deque, or null if this deque is empty
    E pollFirst();

    
Retrieves and removes the last element of this deque, or returns null if this deque is empty.

Returns:
the tail of this deque, or null if this deque is empty
    E pollLast();

    
Retrieves, but does not remove, the first element of this deque. This method differs from peekFirst only in that it throws an exception if this deque is empty.

Returns:
the head of this deque
Throws:
NoSuchElementException if this deque is empty
    E getFirst();

    
Retrieves, but does not remove, the last element of this deque. This method differs from peekLast only in that it throws an exception if this deque is empty.

Returns:
the tail of this deque
Throws:
NoSuchElementException if this deque is empty
    E getLast();

    
Retrieves, but does not remove, the first element of this deque, or returns null if this deque is empty.

Returns:
the head of this deque, or null if this deque is empty
    E peekFirst();

    
Retrieves, but does not remove, the last element of this deque, or returns null if this deque is empty.

Returns:
the tail of this deque, or null if this deque is empty
    E peekLast();

    
Removes the first occurrence of the specified element from this deque. If the deque does not contain the element, it is unchanged. More formally, removes the first element e such that (o==null ? e==null : o.equals(e)) (if such an element exists). Returns true if this deque contained the specified element (or equivalently, if this deque changed as a result of the call).

Parameters:
o element to be removed from this deque, if present
Returns:
true if an element was removed as a result of this call
Throws:
java.lang.ClassCastException if the class of the specified element is incompatible with this deque (optional)
java.lang.NullPointerException if the specified element is null and this deque does not permit null elements (optional)
    boolean removeFirstOccurrence(Object o);

    
Removes the last occurrence of the specified element from this deque. If the deque does not contain the element, it is unchanged. More formally, removes the last element e such that (o==null ? e==null : o.equals(e)) (if such an element exists). Returns true if this deque contained the specified element (or equivalently, if this deque changed as a result of the call).

Parameters:
o element to be removed from this deque, if present
Returns:
true if an element was removed as a result of this call
Throws:
java.lang.ClassCastException if the class of the specified element is incompatible with this deque (optional)
java.lang.NullPointerException if the specified element is null and this deque does not permit null elements (optional)
    boolean removeLastOccurrence(Object o);
    // *** Queue methods ***

    
Inserts the specified element into the queue represented by this deque (in other words, at the tail of this deque) if it is possible to do so immediately without violating capacity restrictions, returning true upon success and throwing an IllegalStateException if no space is currently available. When using a capacity-restricted deque, it is generally preferable to use offer.

This method is equivalent to addLast(java.lang.Object).

Parameters:
e the element to add
Returns:
true (as specified by Collection.add(java.lang.Object))
Throws:
java.lang.IllegalStateException if the element cannot be added at this time due to capacity restrictions
java.lang.ClassCastException if the class of the specified element prevents it from being added to this deque
java.lang.NullPointerException if the specified element is null and this deque does not permit null elements
java.lang.IllegalArgumentException if some property of the specified element prevents it from being added to this deque
    boolean add(E e);

    
Inserts the specified element into the queue represented by this deque (in other words, at the tail of this deque) if it is possible to do so immediately without violating capacity restrictions, returning true upon success and false if no space is currently available. When using a capacity-restricted deque, this method is generally preferable to the add(java.lang.Object) method, which can fail to insert an element only by throwing an exception.

This method is equivalent to offerLast(java.lang.Object).

Parameters:
e the element to add
Returns:
true if the element was added to this deque, else false
Throws:
java.lang.ClassCastException if the class of the specified element prevents it from being added to this deque
java.lang.NullPointerException if the specified element is null and this deque does not permit null elements
java.lang.IllegalArgumentException if some property of the specified element prevents it from being added to this deque
    boolean offer(E e);

    
Retrieves and removes the head of the queue represented by this deque (in other words, the first element of this deque). This method differs from poll only in that it throws an exception if this deque is empty.

This method is equivalent to removeFirst().

Returns:
the head of the queue represented by this deque
Throws:
NoSuchElementException if this deque is empty
    E remove();

    
Retrieves and removes the head of the queue represented by this deque (in other words, the first element of this deque), or returns null if this deque is empty.

This method is equivalent to pollFirst().

Returns:
the first element of this deque, or null if this deque is empty
    E poll();

    
Retrieves, but does not remove, the head of the queue represented by this deque (in other words, the first element of this deque). This method differs from peek only in that it throws an exception if this deque is empty.

This method is equivalent to getFirst().

Returns:
the head of the queue represented by this deque
Throws:
NoSuchElementException if this deque is empty
    E element();

    
Retrieves, but does not remove, the head of the queue represented by this deque (in other words, the first element of this deque), or returns null if this deque is empty.

This method is equivalent to peekFirst().

Returns:
the head of the queue represented by this deque, or null if this deque is empty
    E peek();
    // *** Stack methods ***

    
Pushes an element onto the stack represented by this deque (in other words, at the head of this deque) if it is possible to do so immediately without violating capacity restrictions, returning true upon success and throwing an IllegalStateException if no space is currently available.

This method is equivalent to addFirst(java.lang.Object).

Parameters:
e the element to push
Throws:
java.lang.IllegalStateException if the element cannot be added at this time due to capacity restrictions
java.lang.ClassCastException if the class of the specified element prevents it from being added to this deque
java.lang.NullPointerException if the specified element is null and this deque does not permit null elements
java.lang.IllegalArgumentException if some property of the specified element prevents it from being added to this deque
    void push(E e);

    
Pops an element from the stack represented by this deque. In other words, removes and returns the first element of this deque.

This method is equivalent to removeFirst().

Returns:
the element at the front of this deque (which is the top of the stack represented by this deque)
Throws:
NoSuchElementException if this deque is empty
    E pop();
    // *** Collection methods ***

    
Removes the first occurrence of the specified element from this deque. If the deque does not contain the element, it is unchanged. More formally, removes the first element e such that (o==null ? e==null : o.equals(e)) (if such an element exists). Returns true if this deque contained the specified element (or equivalently, if this deque changed as a result of the call).

This method is equivalent to removeFirstOccurrence(java.lang.Object).

Parameters:
o element to be removed from this deque, if present
Returns:
true if an element was removed as a result of this call
Throws:
java.lang.ClassCastException if the class of the specified element is incompatible with this deque (optional)
java.lang.NullPointerException if the specified element is null and this deque does not permit null elements (optional)
    boolean remove(Object o);

    
Returns true if this deque contains the specified element. More formally, returns true if and only if this deque contains at least one element e such that (o==null ? e==null : o.equals(e)).

Parameters:
o element whose presence in this deque is to be tested
Returns:
true if this deque contains the specified element
Throws:
java.lang.ClassCastException if the type of the specified element is incompatible with this deque (optional)
java.lang.NullPointerException if the specified element is null and this deque does not permit null elements (optional)
    boolean contains(Object o);

    
Returns the number of elements in this deque.

Returns:
the number of elements in this deque
    public int size();

    
Returns an iterator over the elements in this deque in proper sequence. The elements will be returned in order from first (head) to last (tail).

Returns:
an iterator over the elements in this deque in proper sequence
    Iterator<E> iterator();

    
Returns an iterator over the elements in this deque in reverse sequential order. The elements will be returned in order from last (tail) to first (head).

Returns:
an iterator over the elements in this deque in reverse sequence
New to GrepCode? Check out our FAQ X