Instead of using ArrayBuilder, you can use org.apache.spark.util.collection.PrimitiveVector