Download presentation
Presentation is loading. Please wait.
Published byJaven Dains Modified over 10 years ago
1
5/2/2015 Good Java Programming 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
2
5/2/2015 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; }
3
5/2/2015 Good Java Programming 3 Driver Code // print 26 pairs (a a, b b, cc, …). Note that Set does not take duplicate elements Set s = new HashSet (); 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
5/2/2015 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?
5
5/2/2015 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; }
6
5/2/2015 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)
7
5/2/2015 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; }
8
5/2/2015 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?
9
5/2/2015 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; }
10
5/2/2015 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)
11
5/2/2015 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; }
12
5/2/2015 Good Java Programming 12 Driver Code // print 26 pairs (a a, b b, cc, …). Note that Set does not take duplicate elements Set s = new HashSet (); 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?
13
5/2/2015 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
14
5/2/2015 Good Java Programming 14 Design Guideline Consistently use the Override annotation
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.