Converting javax.security.cert.X509Certificate[] to java.security.cert.X509Certificate[] keeping order I have got an array of javax.security.cert.X509Certificate and need to convert it into an array of java.security.cert.X509Certificate. 

My thoughts when I implemented the following code was that I must keep the order of the certificate thus I used a LinkedList. In the first step I am converting all of them into the new data type and appending it to the LinkedList. Afterwards I create temporary array of a size which equals the amount of certificates in LinkedList. Iterating over the LinkedList allows me to put each single certificate into the temp array and finally I can set my my private java.security.cert.X509Certificate array field. I have got the feeling that it is unnecessarily verbose and not efficient and could be shorter.

private X509Certificate[] x509Certificates;

private void setX509Certificates(javax.security.cert.X509Certificate[] certs)
{
  LinkedList&lt;X509Certificate&gt; x509CertificateLinkedList = new LinkedList&lt;&gt;();

  try
  {
    CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");

    for (javax.security.cert.X509Certificate cert : certs)
    {
      try
      {
        ByteArrayInputStream bais = new ByteArrayInputStream(cert.getEncoded());
        X509Certificate x509Certificate = (X509Certificate) certificateFactory.generateCertificate(bais);
        x509CertificateLinkedList.add(x509Certificate);
      }
      catch (CertificateEncodingException e)
      {
        e.printStackTrace();
      }
    }

    X509Certificate[] tempX509Certificates = new X509Certificate[x509CertificateLinkedList.size()];

    for (int i = 0; i &lt; x509CertificateLinkedList.toArray().length; i++)
    {
      if (x509CertificateLinkedList.get(i) != null)
      {
        tempX509Certificates[i] = x509CertificateLinkedList.get(i);
      }
    }

    this.x509Certificates = tempX509Certificates;
  }
  catch (CertificateException e)
  {
    e.printStackTrace();
  }
}
private X509Certificate[] x509Certificates;

private void setX509Certificates(javax.security.cert.X509Certificate[] certs) throws CertificateException {
    CertificateFactory certificateFactory = certificateFactory = CertificateFactory.getInstance("X.509");
    List&lt;X509Certificate&gt; x509List = new ArrayList&lt;&gt;();

    for (javax.security.cert.X509Certificate cert : certs) {
        try {
            ByteArrayInputStream bais = new ByteArrayInputStream(cert.getEncoded());
            X509Certificate x509Certificate = (X509Certificate) certificateFactory.generateCertificate(bais);
            if (x509Certificate != null) {
                x509List.add(x509Certificate);
            }
        } catch (CertificateEncodingException e) {
            e.printStackTrace(System.err);
        }
    }
    this.x509Certificates = x509List.toArray(new X509Certificate[]{});
}


Linked List which you have used for temporary purpose. This could be achieved in a single for loop. Since there will not be any duplicate certificate it is safe to use ArrayList for collecting the certifcates. 

I am not sure your exception handling strategy. So I am throwing the CertificateException you are free to change it based on your exception strategy.