Hello, dear colleagues, I have a task to create a reference book on the HashMap image, and when I started doing methods for adding, inserting, and the like, I ran into a problem: I need an iterator that will first look at the cell, then go through the linked list in it and move on to the next one, and I don't understand how I can do it. It is clear that this should be something like an iterator iterator, and I even did such a task, but those were iterators on arrays, and here I have an external array level, and internal linked lists, and I'm at a dead end.
How do I make such an iterator in this context? I would be very grateful for any help, advice or code ...
Here is the code for the directory itself:
public class ReferenceBook<K, V> implements Book<K, V> { private Node<K, V>[] hashTable; private final float DEFAULT_LOAD_FACTOR = 0.75f; private float threshold = hashTable.length * DEFAULT_LOAD_FACTOR; private int size = 0; ReferenceBook() { hashTable = new Node[16]; } @Override public boolean insert(K key, V value) { return false; } @Override public boolean delete(K key) { return false; } @Override public V get(K key) { return null; } @Override public Iterator<V> iterator() { return new Iterator<V>() { int countArray = 0; // Вот тут корень моих проблем. Подскажите что я могу предпринять чтобы итератор работал как надо? @Override public boolean hasNext() { return currentCellOfTableHaveMoreElement() || arrayTableHaveMoreCell(); } private boolean arrayTableHaveMoreCell() { return countArray < hashTable.length; } private boolean currentCellOfTableHaveMoreElement() { return hashTable[countArray].getNext().getValue() != null; } @Override public V next() { return null; } }; } private class Node<K, V> { private Node<K, V> next; private int hash; private K key; private V value; Node(K key, V value, Node<K, V> next) { this.key = key; this.value = value; this.next = next; initHash(); } private void initHash() { hash = 31; hash = hash * 17 + key.hashCode(); hash = hash * 17 + next.hashCode(); hash = hash * 17 + value.hashCode(); } public Node<K, V> getNext() { return next; } public K getKey() { return key; } public V getValue() { return value; } @Override public int hashCode() { return hash; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj instanceof Node<?, ?>) { Node<K, V> node = (Node) obj; return (Objects.equals(key, node.getKey()) && Objects.equals(value, node.getValue()) || Objects.equals(hash, node.hashCode())); } return false; } } }