title: How to send ArrayList of Objects in Kafka Avro producer without calling send method for every individual record?
body: <p>I am using the below code to produce Avro records of <code>User</code> class into Kafka topic, and it is working fine;</p>
  
  <h2>Sender class</h2>
  
  <pre><code>import org.apache.avro.Schema;
  import org.apache.avro.generic.GenericData;
  import org.apache.avro.generic.GenericDatumReader;
  import org.apache.avro.generic.GenericRecord;
  import org.apache.avro.io.DecoderFactory;
  import org.apache.avro.io.EncoderFactory;
  import org.apache.avro.reflect.ReflectData;
  import org.apache.avro.reflect.ReflectDatumWriter;
  import org.apache.kafka.clients.producer.KafkaProducer;
  import org.apache.kafka.clients.producer.ProducerConfig;
  import org.apache.kafka.clients.producer.ProducerRecord;
  
  import java.io.ByteArrayOutputStream;
  import java.io.IOException;
  import java.util.Properties;
  
  import vo.User;
  
  public class Sender8 {
  
      public static void main(String[] args) {
  
          User user = new User(10,"testName");
          Schema schema = ReflectData.get().getSchema(user.getClass());
          new GenericData.Record(schema);
  
          Properties props = new Properties();
          props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "127.0.0.1:9092");
          props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG,io.confluent.kafka.serializers.KafkaAvroSerializer.class);
          props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG,io.confluent.kafka.serializers.KafkaAvroSerializer.class);
          props.put("schema.registry.url", "http://127.0.0.1:8081");
  
          KafkaProducer&lt;String, GenericRecord&gt; producer = new KafkaProducer&lt;String, GenericRecord&gt;(props);
  
          ReflectDatumWriter&lt;Object&gt; reflectDatumWriter = new ReflectDatumWriter&lt;&gt;(schema);
          GenericDatumReader&lt;Object&gt; genericRecordReader = new GenericDatumReader&lt;&gt;(schema);
          ByteArrayOutputStream bytes = new ByteArrayOutputStream();
  
          try {
              reflectDatumWriter.write(user, EncoderFactory.get().directBinaryEncoder(bytes, null));
              GenericRecord avroRecord2 = (GenericRecord) genericRecordReader.read(null, DecoderFactory.get().binaryDecoder(bytes.toByteArray(), null));
              ProducerRecord&lt;String, GenericRecord&gt; record = new ProducerRecord&lt;String, GenericRecord&gt;("avrotesttopic1", avroRecord2);
              producer.send(record);
              producer.flush();
  
          } catch (IOException e1) {
              e1.printStackTrace();
          }
  
          producer.close();
      }
  }
  </code></pre>
  
  <h2>User class</h2>
  
  <pre><code>public class User {
      int id;
      String name;
  
      public User(int id, String name) {
          super();
          this.id = id;
          this.name = name;
      }
  
      public int getId() {
          return id;
      }
  
      public void setId(int id) {
          this.id = id;
      }
  
      public String getName() {
          return name;
      }
  
      public void setName(String name) {
          this.name = name;
      }
  }
  </code></pre>
  
  <p>Sometimes, I might need to send a collection of objects as an arraylist, like;</p>
  
  <pre><code>ArrayList&lt;User&gt; users = new ArrayList&lt;User&gt;();
  </code></pre>
  
  <p>in this situation, what I does is, creates a loop to iterate through the list, pick individual records, and call <code>send()</code> method, like;</p>
  
  <pre><code>Iterator iter = users.iterator();
  while (iter.hasNext()) {
     user = iter.next();
     //all other stuff here
     producer.send(record);
  }
  </code></pre>
  
  <p>This works fine. But the problem is, if my arraylist has 50 records, <code>producer.send(record)</code> will be triggered 50 times. I would like to know if there is any other more efficient method to handle this, like calling sender only once, for all 50 records.</p>