call trimToSize in build() method to reduce memory footprint.
fix bug that causes ClassCastException.