Java: immutable Stack?
- by HH
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
How to get immutable Stack data structure? Can I box it with list?
Should I switch my current implementatios from stacks to Lists to get immutable?
Which immutable data structure is Very fast with about similar exec time as Stack?