Mostrando entradas con la etiqueta java. Mostrar todas las entradas
Mostrando entradas con la etiqueta java. Mostrar todas las entradas

miércoles, 10 de mayo de 2023

ArrayList vs LinkedList in Java: A Deeper Comparison

 To understand the differences between ArrayList and LinkedList in Java, it's first important to understand that both are implementations of the List interface in Java and therefore offer similar functionalities. However, their performance in various situations differs due to the underlying data structures they use.

ArrayList


ArrayList is a dynamic array implementation. At its core, an ArrayList uses an array to store its elements. However, this array can grow dynamically to accommodate new elements as they are added to the list, which is not possible with a regular array.

The primary advantage of using an ArrayList lies in its efficiency in retrieving data. Thanks to the nature of arrays, you can access any element of an ArrayList in constant time (O(1)), simply by referencing the index of the element. This makes it ideal for scenarios where you need to frequently access elements by their index.
However, the insertion and deletion of elements in an ArrayList can be costly in terms of performance. If you insert or delete an element anywhere other than the end of the list, all subsequent elements must be shifted to fill or make space for the new element. This results in a time complexity of O(n).

LinkedList


LinkedList, on the other hand, is an implementation of a doubly-linked list. In a linked list, each element contains a reference to the previous and next elements in the list, forming a chain of elements.

The biggest advantage of a LinkedList is that it allows efficient insertions and deletions. You can add or remove an element from the list simply by changing the references of the neighboring elements. This process has a constant time complexity O(1) if the insertion or deletion occurs at the beginning or end of the list. However, if it occurs in the middle, we first need to locate the insertion or deletion point, which has a time complexity of O(n).

Contrarily, accessing individual elements in a LinkedList is slower compared to an ArrayList. Since the elements are not indexed, to access a particular element, you have to traverse the list from the beginning (or from the end, if it's closer), which has a time complexity of O(n).

Summary


In summary, if your application requires frequent index-based access to elements, an ArrayList might be the better choice. If, on the other hand, your application requires frequent insertions and deletions, especially in the middle of the list, then a LinkedList might be more suitable.

Additionally, it's important to note that LinkedList has a higher memory footprint than ArrayList because, in addition to the data, LinkedList also stores two references for the previous and next node.

Understanding these differences and how they impact the performance of your application will help you make an informed decision about which data structure to use in your Java code.

sábado, 21 de noviembre de 2020

JAVA Codify AES /JAVA Codificación AES

 El siguiente es un ejemplo de codificación AES en Java v8.

The next is an example of JAVA AES Encoding in Java 8


import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;

public class Codify {
    public String codifyAES(String arg)   {


        try {
            byte[] raw = new byte[]{'0', '4', '3', '8', '4', '2', 'A', '5', '6', '0', '0', '0', '9', '8', '1', '9'};
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
            Cipher cipher = null;
            cipher = Cipher.getInstance("AES");
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
            byte[] encrypted = cipher.doFinal(arg.getBytes());
            return Base64.getEncoder().encodeToString(encrypted);
        } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException e) {
            e.printStackTrace();
        }

        return null;
    }

}

AES 256

private static String secretKey = "boooooooooom!!!!";
private static String salt = "ssshhhhhhhhhhh!!!!";
 
public static String encrypt(String strToEncrypt, String secret) 
{
    try
    {
        byte[] iv = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
        IvParameterSpec ivspec = new IvParameterSpec(iv);
         
        SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
        KeySpec spec = new PBEKeySpec(secretKey.toCharArray(), salt.getBytes(), 65536, 256);
        SecretKey tmp = factory.generateSecret(spec);
        SecretKeySpec secretKey = new SecretKeySpec(tmp.getEncoded(), "AES");
         
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivspec);
        return Base64.getEncoder().encodeToString(cipher.doFinal(strToEncrypt.getBytes("UTF-8")));
    } 
    catch (Exception e) 
    {
        System.out.println("Error while encrypting: " + e.toString());
    }
    return null;
}