我會嘗試着正確的點。自定義對象比較器
我有我的自定義節點對象,它具有屬性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;
}
}
這裏是我的節點類:
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;
}
}
'Set'默認 - 消除重複。你需要在你的'Node'類中覆蓋你的'equals()'''hashCode()'。 – SudoRahul 2013-03-26 11:21:41
@ R.J:您應該將其作爲答案發布。 – Keppil 2013-03-26 11:23:48
@Keppil - 完成! – SudoRahul 2013-03-26 11:25:34