fork download
  1. import java.util.*;
  2. import java.io.*;
  3.  
  4. class Example {
  5. public static void main(String[] args) throws IOException {
  6. final int count = 1000;
  7.  
  8. ArrayList<Boolean> list = new ArrayList<>(count);
  9. list.addAll(Collections.nCopies(count, Boolean.TRUE));
  10. System.out.printf("ArrayList: %d%n", sizeOf(list));
  11.  
  12. boolean[] array = new boolean[count];
  13. Arrays.fill(array, true);
  14. System.out.printf("boolean[]: %d%n", sizeOf(array));
  15.  
  16. BitSet bits = new BitSet(count);
  17. bits.set(0, count);
  18. System.out.printf("BitSet: %d%n", sizeOf(bits));
  19. }
  20.  
  21. static int sizeOf(Serializable obj) throws IOException {
  22. ObjectOutputStream objsOut = new ObjectOutputStream(bytesOut);
  23. objsOut.writeObject(obj);
  24. return bytesOut.toByteArray().length;
  25. }
  26. }
Success #stdin #stdout 0.15s 320448KB
stdin
Standard input is empty
stdout
ArrayList: 5096
boolean[]: 1027
BitSet: 201