Java: immutability, overuse of stack -- better data structure?

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 7:38 UTC
Read the original article Hit count: 580

I overused hashSets but it was slow, then changed to Stacks, speed boost-up. Poly's reply uses Collections.emptyList() as immutable list, cutting out excess null-checkers. No Collections.emptyStack(). Combining the words stack and immutability, from the last experiences, gets "immutable stack" (probably not related to functional prog).

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. The first hinted of misusing stack. The lack of immutabily in the last and poly's book recommendation leads way to list. Something very primitive, fast, no extra layers, with methods like emptyThing().

Overuse of stack and where I use it  public Stack<DataFile> files;        public Stack<String> printViews = new Stack<String>();      private static Stack<Object> getFormat(File f){return (new Format(f)).getFormat();}            private Stack<Object> getLine(File[] fs,String s){return wF;}            private Stack<Object> getFormat(){return format;} public static Stack<Integer[]> getPrintPoss(String s,File f,Integer maxViewPerF)         Stack<File> possPrint = new Stack<File>();         Stack<Integer> positions=new Stack<Integer>();    private String getFormatLine(Stack<Object> st)            Stack<String> lines=new Stack<String>();      public static final Stack<File> allFiles = findf.getFs();      public static final Stack<File> allDirs = findf.getDs();      private Stack<Integer[]> positionsPrint=new Stack<Integer[]>();      public Stack<String> getSearchResults(String s, Integer countPerFile, Integer resCount)              Stack<File> filesToS=Fs2Word.getFs2W(s,50);              Stack<String> rs=new Stack<String>();      public Stack<Integer[]> poss = new Stack<Integer[4]>();      public static Stack<String> getPrintViewsFileWise(String s,Object[] df,Integer maxViewsPerF)              Stack<String> substrings = new Stack<String>();              private Stack<String> printViews=new Stack<String>();              MatchView(Stack<Integer> pss,File f,Integer maxViews)                      Stack<String> formatFile;                      private Stack<Search> files;                      private Stack<File> matchingFiles;                      private Stack<String> matchViews;                      private Stack<String> searchMatches;                      private Stack<String> getSearchResults(Integer numbResults)

Easier with List:

  • AllDirs and AllFs, now looping with push, but list has more pow. methods such as addAll


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?

No immutability to Stack with Final

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>();

                //WHY is there not an error to remove an elment
                // from FINAL stack?

© Stack Overflow or respective owner

Related posts about java

Related posts about immutable