Java: immutable Stack?

Posted by HH on Stack Overflow See other posts from Stack Overflow or by HH
Published on 2010-05-06T05:50:52Z Indexed on 2010/05/06 5:58 UTC
Read the original article Hit count: 617

Filed under:
|
|

I chose to use Stacks and Tables before knowing Collections has immutable empty things only for Set, Map and List. Because the size of table does not change after its init:

Integer[] table = new Intger[0]

I can use the zero-witdh table as an empty table. But I cannot use final or empty Stack to get immutable Stack:

No immutability to Stack with Final

import java.io.*;
import java.util.*;

public class TestStack{


        public static void main(String[] args)
        {
                final Stack<Integer> test = new Stack<Integer>();
                Stack<Integer> test2 = new Stack<Integer>();
                test.push(37707);
                test2.push(80437707);

                //WHY is there not an error to remove an elment
                // from FINAL stack?
                System.out.println(test.pop());
                System.out.println(test2.pop());
        }
}

Java Api 5 for list interface shows that Stack is an implementing class for list and arraylist, here. The java.coccurrent pkg does not have any immutable Stack data structure.

From Stack to some immutable data structure

  1. How to get immutable Stack data structure? Can I box it with list?
  2. Should I switch my current implementatios from stacks to Lists to get immutable?
  3. Which immutable data structure is Very fast with about similar exec time as Stack?

© Stack Overflow or respective owner

Related posts about java

Related posts about immutable