I want to hash an array of strings into one hash as shown on scheme on the image below. But I do not want to implement some class based Merkle tree. It's just a simple function. Is this a good implementation or are there any possible improvements?
public static String hashStrings(@NotNull String [] strs)
{
if (strs.length == 1) return hashString(strs[0]);
List<String> hashes = new ArrayList<>();
for (int i = 0; i < strs.length; i++)
{
hashes.add(hashString(strs[i]));
}
while(hashes.size() > 1)
{
int currentSize = hashes.size();
int nextSize = currentSize % 2 == 0 ? currentSize / 2 : (currentSize + 1) / 2;
for (int i = 0; i < currentSize - 1; i += 2)
{
hashes.set(i / 2, hashString(hashes.get(i) + hashes.get(i + 1)));
}
if (currentSize % 2 == 1)
{
hashes.set(nextSize - 1, hashString(hashes.get(currentSize - 1)));
}
hashes = hashes.subList(0, nextSize);
}
return hashes.get(0);
}