1 / 14

Bigram: group/word of 2 letters

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.

danton
Download Presentation

Bigram: group/word of 2 letters

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. 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

  3. 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

  4. 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

  5. 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

  6. 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

  7. 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

  8. 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

  9. 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

  10. 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

  11. 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

  12. 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

  13. 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

  14. Design Guideline Consistently use the Override annotation Good Java Programming

More Related