140 likes | 250 Views
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. Code for Bigram class: override equals.
E N D
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 Good Java Programming
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; } } Good Java Programming
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? Good Java Programming
Driver Code: output? First :a Second: a First :b Second :b …. The size of the Hashset is 2600 Perhaps the override did not work? Good Java Programming
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; } } Good Java Programming
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) Good Java Programming
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; } Good Java Programming
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? Good Java Programming
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; } } Good Java Programming
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) Good Java Programming
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; } } Good Java Programming
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? Good Java Programming
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 Good Java Programming
Design Guideline Consistently use the Override annotation Good Java Programming