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: 1218

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

DataFile.java:  public Stack<DataFile> files;
FileObject.java:        public Stack<String> printViews = new Stack<String>();
FileObject.java://      private static Stack<Object> getFormat(File f){return (new Format(f)).getFormat();}
Format.java:            private Stack<Object> getLine(File[] fs,String s){return wF;}
Format.java:            private Stack<Object> getFormat(){return format;}
Positions.java: public static Stack<Integer[]> getPrintPoss(String s,File f,Integer maxViewPerF)
Positions.java:         Stack<File> possPrint = new Stack<File>();
Positions.java:         Stack<Integer> positions=new Stack<Integer>();
Record.java:    private String getFormatLine(Stack<Object> st)
Record.java:            Stack<String> lines=new Stack<String>();
SearchToUser.java:      public static final Stack<File> allFiles = findf.getFs();
SearchToUser.java:      public static final Stack<File> allDirs = findf.getDs();
SearchToUser.java:      private Stack<Integer[]> positionsPrint=new Stack<Integer[]>();
SearchToUser.java:      public Stack<String> getSearchResults(String s, Integer countPerFile, Integer resCount)
SearchToUser.java:              Stack<File> filesToS=Fs2Word.getFs2W(s,50);
SearchToUser.java:              Stack<String> rs=new Stack<String>();
View.java:      public Stack<Integer[]> poss = new Stack<Integer[4]>();
View.java:      public static Stack<String> getPrintViewsFileWise(String s,Object[] df,Integer maxViewsPerF)
View.java:              Stack<String> substrings = new Stack<String>();
View.java:              private Stack<String> printViews=new Stack<String>();
View.java:              MatchView(Stack<Integer> pss,File f,Integer maxViews)
View.java:                      Stack<String> formatFile;
View.java:                      private Stack<Search> files;
View.java:                      private Stack<File> matchingFiles;
View.java:                      private Stack<String> matchViews;
View.java:                      private Stack<String> searchMatches;
View.java:                      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

[OLD]

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

© Stack Overflow or respective owner

Related posts about java

Related posts about immutable