私は要点を正しく理解しようとします。
Cost属性を持つカスタムNodeオブジェクトがあります。これらのノードオブジェクトを属性Costの昇順で並べ替えたいと思います。
を使用してこれを行うことはできましたPriorityQueue<Node> = new PriorityQueue<Node>(10000, new NodeComparator());
が、その方法は私には遅すぎました。現在、TreeSetのみを使用して、同じことを実行しようとしています。とにかく、私のコンストラクターがこのように見える場合TreeSet<Node> = new TreeSet<Node>(new NodeComparator());
、プログラムは大量のNodeオブジェクトをスキップしているように見え、それらを同じものとして扱っているように見えます。彼らはそうではありません。hashCodeの問題があるかもしれないと思いますが、よくわかりません。現時点では解決方法がわかりません。
簡潔にするために、TreeSet内のノードをCost属性の昇順で並べ替えたいだけです。これが私のNodeComparatorクラスです:
public class NodeComparator implements Comparator<Node> {
@Override
public int compare(Node n1, Node n2) {
// TODO Auto-generated method stub
if(n1.cost > n2.cost) return 1;
else if(n1.cost < n2.cost) return -1;
else return 0;
}
}
そして、これが私のNodeクラスです。
public class Node{
public State state;
public int cost;
public Node(State s, int Cost){
this.state = s;
this.cost = Cost;
}
public State getState(){
return this.state;
}
public int getCost(){
return this.cost;
}
}
私はあなたに私の州のクラスも提供します。
public class State {
public int lamp;
public ArrayList<Integer> left;
public State(ArrayList<Integer> Left, int Lamp){
lamp = Lamp;
left = Left;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + lamp;
result = prime * result + ((left == null) ? 0 : left.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
State other = (State) obj;
if (lamp != other.lamp)
return false;
if (left == null) {
if (other.left != null)
return false;
} else if (!left.equals(other.left))
return false;
return true;
}
}