Correct and rewrite the equals method compareTo method


I. Overview


Program to sort a bunch of data elements, search, delete increase.
Data Node

class Node{
        int type;
        int index;
        int score;
}

  

Rule:
  1) object is equal to: two nodes n1 and n2, if n1.type == n2.type && n1.index == n2.index n1 is equal to N2
  2) Sort: Ascending comparison score, the comparison score same type , type the same as the comparative index.
initially I use TreeMap storage. Implement the Comparable interface, and override the equals method hashCode method.
as follows:

class Node implements Comparable<Node>{
	public int type;
	public int index;
	public int score;
	public Node(int t, int u, int s) {
		this.type = t;
		this.index = u;
		this.score = s;
	}
	@Override
	public int compareTo(Node o) {
		if(this.score != o.score) return this.score > o.score ? -1 : 1;
		else if(this.type != o.type) return this.type - o.type;
		else return this.index - o.index;
	}
	@Override
	public boolean equals(Object obj) {
		// TODO Auto-generated method stub
		if(this == obj) return true;
		if(obj instanceof Node) {
			Node tn = (Node) obj;
			if(tn.type == this.type && tn.index == this.index) return true;
		}
		return false;
	}
	@Override
	public int hashCode() {
		// TODO Auto-generated method stub
		return this.type + this.index;
	}
}

Program has been wrong, after two hours of repeated examination. I realized that if TreeMap comparison is the same as calling the CompareTo method. equals and hashCode HashMap that is set.
After modification, each type of data stored by a TreeMap.
as follows:

class Node implements Comparable<Node>{
	public int type;
	public int index;
	public int score;
	public Node(int t, int u, int s) {
		this.type = t;
		this.index = u;
		this.score = s;
	}
	@Override
	public int compareTo(Node o) {
		if(this.type == o.type && this.index == o.index) return 0;
		else {
			if(this.score != o.score) return this.score > o.score ? -1 : 1;
			else return this.index - o.index;
		}
	}
}

The final sorting using a priority queue.
Comparators:

Comparetor<Node> cmp = (x, y) ->{
	if(x.score != y.score) return x.score > y.score ? -1: 1;
	else if(x.type != y.type) return x.type - y.type;
	return x.index - y.index;
}

Proper use of equals and compareTo, reduce the bug.


Second, the override equals


HashSet stored in their definition of the object, HashMap using custom objects as Key, need to rewrite equals. At the same time to rewrite hashCode.
HashCode positioning, equals comparison is the same.
as follows:

@Override 
public boolean the equals (Object obj) {// parameter must be of type Object, otherwise invalid 
	// TODO Auto-Generated Stub Method, 
	IF (the this == obj) return to true; // same reference 
	if (obj instanceof Node) {/ / obj is null, the condition is false. 
		TN = the Node (the Node) obj; 
		IF (tn.type == == this.type && tn.index this.index) return to true; // The content comparison target 
	} 
	return to false; 
}

To ensure the same hashCode object returns the same value. I want to achieve a good hashCode more difficult.


Third, rewrite compareTo


Ordered set, to save custom objects have to be rewritten or compareTo method comparator is provided for the object. Common to the collection have TreeMap (red-black tree), TreeSet, PriorityQueue (heap), Arrays :: sort (array sorting), Collections :: sort (List sorting).
as follows:

class Data implements Comparable <Data> { // implement Comparable interface 
	@Override 
	public int the compareTo (the Data O) {// return a negative value less than, equal to 0 returns, returns a positive value greater than 
		// Generated Method Stub the TODO Auto- 
		return 0; 
	} 
}

A comparator, as follows:

Comparator<Node> cmp = new Comparator<Node>() {
		@Override
		public int compare(Node o1, Node o2) {
			// TODO Auto-generated method stub
			return 0;
		}
	};


Use Lambda expressions.

Comparetor<Node> cmp = (x, y) ->{
	if(x.score != y.score) return x.score > y.score ? -1: 1;
	else if(x.type != y.type) return x.type - y.type;
	return x.index - y.index;
}

Guess you like

Origin www.cnblogs.com/suen061/p/11524959.html
Recommended