Override

advertisement
Bigram: group/word of 2 letters
 java.util.Set: A collection that contains no duplicate
elements.
 sets contain no pair of elements e1 and e2 such that
e1.equals(e2),
 and at most one null element.

HashSet uses a HashMap instance for
imnplementation
4/13/2015
Good Java Programming
1
Code for Bigram class: override
equals
public class Bigram {
private final char first;
private final char second;
public Bigram(char firstIn, char secondIn) {
this.first = firstIn;
this.second = secondIn;
}
// override the default equals method
public boolean equals (Bigram b) {
return ( (b.first == first) && (b.second == second) );
}
public int hashcode(){
return 31* first + second;
}
public String toString() {
String printString = "First: " + first + "\t Second: " + second;
return printString;
}
}
4/13/2015
Good Java Programming
2
Driver Code
// print 26 pairs (a a, b b, cc, …) . Note that Set does not take duplicate elements
Set<Bigram> s = new HashSet<Bigram>();
for (int i=0; i<100; i++) {
for (char ch = 'a'; ch <='z'; ch++) {
s.add(new Bigram(ch, ch));
}
}
for (Bigram e : s) {
System.out.println(e);
}
System.out.println("The size of the Hashset is: " + s.size());
What will be the output of this code?
4/13/2015
Good Java Programming
3
Driver Code: output?
First :a Second: a
First :b Second :b
….
The size of the Hashset is 2600
Perhaps the override did not work?
4/13/2015
Good Java Programming
4
Code for Bigram class: override
annotation
public class Bigram {
private final char first;
private final char second;
public Bigram(char firstIn, char secondIn) {
this.first = firstIn;
this.second = secondIn;
}
// override the default equals method
@Override
public boolean equals (Bigram b) {
return ( (b.first == first) && (b.second == second) );
}
public int hashcode(){
return 31* first + second;
}
public String toString() {
String printString = "First: " + first + "\t Second: " + second;
return printString;
}
}
4/13/2015
Good Java Programming
5
Compilation error!
Bigram.java:16: method does not override or implement a
method from a supertype
[javac]
@Override public boolean equals (Bigram b) {
[javac]
^
[javac] 1 error
Instead of overriding, we overloaded the equals method!!
Caught by the compiler (Java 1.5 or newer)
4/13/2015
Good Java Programming
6
Fix the overridden method
// override the default equals method
@Override public boolean equals (Object obj) {
boolean returnValue = false;
if (obj instanceof Bigram) {
Bigram b = (Bigram)obj;
returnValue = (b.first == first) &&
(b.second == second);;
}
return returnValue;
}
4/13/2015
Good Java Programming
7
Driver Code: output. Didn’t fix the
bug?
First :a Second: a
First :b Second :b
….
The size of the Hashset is 2600
Perhaps the override did not work?
4/13/2015
Good Java Programming
8
Code for Bigram class: override
equals
public class Bigram {
private final char first;
private final char second;
public Bigram(char firstIn, char secondIn) {
this.first = firstIn;
this.second = secondIn;
}
// override the default equals method
public boolean equals (Bigram b) {
…
}
@Override public int hashcode(){
return 31* first + second;
}
public String toString() {
String printString = "First: " + first + "\t Second: " + second;
return printString;
}
}
4/13/2015
Good Java Programming
9
Compilation error!
Bigram.java:34: method does not override or implement a
method from a supertype
[javac]
@Override public int hashcode ()
[javac]
^
[javac] 1 error
Instead of overloaded the hashCode method, we defined
another method named hashcode
Caught by the compiler (Java 1.5 or newer)
4/13/2015
Good Java Programming
10
Code for Bigram class: override
equals
public class Bigram {
private final char first;
private final char second;
public Bigram(char firstIn, char secondIn) {
this.first = firstIn;
this.second = secondIn;
}
// override the default equals method
public boolean equals (Bigram b) {
…
}
@Override public int hashCode(){
return 31* first + second;
}
public String toString() {
String printString = "First: " + first + "\t Second: " + second;
return printString;
}
}
4/13/2015
Good Java Programming
11
Driver Code
// print 26 pairs (a a, b b, cc, …) . Note that Set does not take duplicate elements
Set<Bigram> s = new HashSet<Bigram>();
for (int i=0; i<100; i++) {
for (char ch = 'a'; ch <='z'; ch++) {
s.add(new Bigram(ch, ch));
}
}
for (Bigram e : s) {
System.out.println(e);
}
System.out.println("The size of the Hashset is: " + s.size());
What will be the output of this code?
4/13/2015
Good Java Programming
12
Correct output
First: l
Second: l
First: n
Second: n
First: h
Second: h
First: y
Second: y
First: j
Second: j
First: d
Second: d
First: w
Second: w
First: f
Second: f
First: u
Second: u
First: s
Second: s
First: b
Second: b
First: q
Second: q
First: m
Second: m
First: o
Second: o
First: i
Second: i
First: z
Second: z
First: k
Second: k
First: x
Second: x
First: v
Second: v
First: e
Second: e
First: t
Second: t
First: g
Second: g
First: r
Second: r
First: a
Second: a
First: p
Second: p
First: c
Second: c
The size of the Hashset is: 26
4/13/2015
Good Java Programming
13
Design Guideline
Consistently use the Override annotation
4/13/2015
Good Java Programming
14
Download