Replacing static HashSet<String> with Java 7 string switch?
Has anyone investigated whether Java 7 switch is more memory efficient than HashSet<String> or ImmutableSet<String>?  I think it might be.  Read on....
I ask because HashSet is one of the more bloated java collection classes.  It uses 32 bytes for each used entry + an array for the capacity.  For example the follow
ing HashSet uses 128 bytes (2*32 + 16 * 4):
  private static final HashSet<String> FOO = new HashSet<>();
  foo.add("a")
shpub note --category=googplus  --syndication=https://plus.google.com/107786897865850743842/posts/VPYQiMunRRY   --published="Mon Dec 29 12:22:44 PM PST 2014"   --name="" - <<EOF
Replacing static HashSet<String> with Java 7 string switch?
Has anyone investigated whether Java 7 switch is more memory efficient than HashSet<String> or ImmutableSet<String>?  I think it might be.  Read on....
I ask because HashSet is one of the more bloated java collection classes.  It uses 32 bytes for each used entry + an array for the capacity.  For example the follow
ing HashSet uses 128 bytes (2*32 + 16 * 4):
  private static final HashSet<String> FOO = new HashSet<>();
  foo.add("a")
  foo.add("b");
  boolean isFoo(String s) { return FOO.contains(s);}
We can replace that with:
  boolean isFoo(String s) {
     switch (s) {
        case "a":
        case "b":
           return true;
        default:
           return false;
     }
  }
Java 7 transforms this code into something like this:
    switch (s.hashCode()) {
       case -1234:
          return s.equals("a");
       case -55999:
          return s.equals("b");
       default:
          return false;
    }
Which maps to a tableswitch or lookupswitch.  Squinting it appears that you'd need the following for each entry:
tableswitch:
  per-entry: hashcode + result value (8 bytes)
  equals test: 4 opcodes (16 bytes)
So it would appear that you have 24bytes/entry.
Performance would be limited by the JVM/Dalvik/Art implementation of tableswitch which can be O(n)
Also obligatory reference to:
  http://blog.jamesdbloom.com/JavaCodeToByteCode_PartOne.html
http://blog.jamesdbloom.com/JavaCodeToByteCode_PartOne.html
#googplus
        
            
                         #googplus