1. What's the Node class? I've tried googling this but the explanations are all very technical and refer to other things I also don't understand.
2. What's up with the for loop? I've never seen the .next in a for loop like this.
3. What is the hash method accomplishing? It seems it would return a semi-random number between 0 and buckets.length, since hashCode doesn't return a predictable value. if so, wouldn't it just be easier to use the built-in Random class?
4. I'm not even really sure what the get method does here.
I can't even begin trying to solve this until I understand what the existing code is doing.
package com.codegym.task.task26.task2609;
/*
Distributing items into baskets with their own lock
*/
public class Solution
{
private static final int LOCK_COUNT = 12;
private final Node[] buckets;
private final Object[] locks;
static class Node
{
public Node next;
public Object key;
public Object value;
}
public Solution(int bucketCount)
{
buckets = new Node[bucketCount];
locks = new Object[LOCK_COUNT];
for (int i = 0; i < LOCK_COUNT; i++)
{
locks[i] = new Object();
}
}
private final int hash(Object key)
{
return Math.abs(key.hashCode() % buckets.length);
}
public Object get(Object key)
{
int hash = hash(key);
synchronized (locks[locks.length-1])
{
for (Node m = buckets[hash]; m != null; m = m.next)
{
if (m.key.equals(key)) return m.value;
}
}
return null;
}
public void clear()
{
for (int i = 0; i < buckets.length; i++)
{
synchronized (locks[locks.length - (i + 1)])
{
buckets[i] = null;
}
}
}
public static void main(String[] args)
{
}
}