我试图为类似于LinkedList但以递归方式管理的类编写hashCode,我有点写一个方法,但我不认为它是最精确的以递归方式编写哈希码的方法。 有什么建议? 你认为平等是正确的吗? 这是代码,因此您可以了解该类的工作原理:
package poo.recursion;
import poo.util.CollezioneOrdinata;
import java.util.Iterator;
import java.util.NoSuchElementException;
public class ListaRec <T extends Comparable<? super T>> implements CollezioneOrdinata<T> {
private static class List<E>{
E info;
List<E> next; //because every node can be considered as a sub-list
}
private List<T> lista = null;
public int size(){
return size(lista);
}
private int size(Lista<T> lista){
if(lista == null)
return 0;
return 1+size(lista.next);
}
public boolean contains(T e){
return contains(lista,e);
}
private boolean contains(Lista<T> lista,T e){
if(lista == null)
return false;
if(lista.info.equals(e))
return true;
if(lista.info.compareTo(e) > 0) //because the list is sorted
return false;
return contains(lista.next,e);
}
public T get(T e){
return get(lista,e);
}
private T get(Lista<T> lista,T e){
if(lista == null)
return null;
if(lista.info.equals(e))
return lista.info;
if(lista.info.compareTo(e) > 0)
return null;
return get(lista.next,e);
}
public void add(T e){
lista = add(lista,e);
}
private Lista<T> add(Lista<T> lista,T e){
if(lista == null){
lista = new Lista<T>();
lista.info = e;
return lista;
}
if(lista.info.compareTo(e) >= 0){
Lista<T> n = new Lista<>();
n.info = e;
n.next = lista;
return n;
}
lista.next = add(lista.next,e);
return lista;
}
@Override
@SuppressWarnings("unchecked")
public boolean equals(Object o){
if (!(o instanceof ListaRec))
return false;
if (o == this)
return true;
ListaRec<T> l = (ListaRec<T>) o;
int lSize = l.size();
int thisSize = this.size();
if (lSize != thisSize)
return false;
if (lSize != 0) {
return equals(l.lista,this.lista);
}
return true; //because it means that they are empty
}
private boolean equals(Lista<T> object,Lista<T> lista) {
if(!object.info.equals(lista.info))
return false;
if(object.next == null)
return true;
return equals(object.next,lista.next);
}
@Override
public int hashCode() {
return hashCode(lista);
}
private int hashCode(Lista<T> lis){
final int PRIME = 43;
if(lis.next == null)
return 1;
return PRIME * hashCode(lis.next) + lista.info.hashCode();
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder(500);
sb.append('[');
toString(lista,sb);
if(sb.length() >1)
sb.setLength(sb.length()-2);
sb.append(']');
return sb.toString();
}
private void toString(Lista<T> lista,StringBuilder sb){
if(lista == null)
return;
sb.append(lista.info);
sb.append(", ");
toString(lista.next,sb);
}
public Iterator<T> iterator() {
return new IteratoreListaRec();
}
private class IteratoreListaRec implements Iterator<T>{
private Lista<T> cor = null, pre = null;
@Override
public boolean hasNext() {
if(cor == null)
return lista != null;
return lista.next != null;
}
@Override
public T next() {
if(!hasNext())
throw new NoSuchElementException();
if(cor == null)
cor = lista;
else{
pre = cor;
cor = cor.next;
}
return cor.info;
}
@Override
public void remove() {
if(pre == cor)
throw new IllegalStateException();
if(lista == cor)
lista = cor.next;
else
pre.next = cor.next;
cor = pre;
}
}
public static void main(String[] args){
ListaRec<Integer> l1 = new ListaRec<>();
ListaRec<Integer> l2 = new ListaRec<>();
l1.add(1);
l1.add(2);
l1.add(3);
l2.add(2);
l2.add(2);
l2.add(3);
System.out.print(l1.hashCode());
}
}