Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Equal_HashCode_TEST --- imporatnt concept about hashing
//'main' method must be in a class 'Rextester'. import java.util.*; import java.lang.*; // abstract so -> can't make its object abstract class Element { int intVal; double doubleVal; String stringVal; Element(final int intVal, final double doubleVal, final String stringVal) { this.intVal = intVal; this.doubleVal = doubleVal; this.stringVal = stringVal; } } class Element_Overridden extends Element { Element_Overridden(final int intVal, final double doubleVal, final String stringVal) { super(intVal, doubleVal, stringVal); } @Override public boolean equals(Object obj) { Element_Overridden e = (Element_Overridden) obj; // DOWNCASTING if (this.intVal == e.intVal && this.doubleVal == e.doubleVal && this.stringVal.equals(e.stringVal)) return true; else return false; } @Override public int hashCode() { return this.intVal; } } class Element_NOT_Overridden extends Element { Element_NOT_Overridden(final int intVal, final double doubleVal, final String stringVal) { super(intVal, doubleVal, stringVal); } } class Rextester { public static void main(String[] args) { test_Element_Overridden(); test_Element_NOT_Overridden(); } public static void test_Element_NOT_Overridden() { System.out.println("\n\n\n\t -- TEST : NOT OVERRIDDEN -- \n"); HashSet<Element_NOT_Overridden> hSet = new HashSet<>(); hSet.add(new Element_NOT_Overridden(1, 23, "first")); hSet.add(new Element_NOT_Overridden(2, 62, "second")); hSet.add(new Element_NOT_Overridden(3, 25, "third")); // --> NOTE : we have fucked with the intVal parameter --> IMPORTANT CONCEPT hSet.add(new Element_NOT_Overridden(1, 53, "fifth")); hSet.add(new Element_NOT_Overridden(2, 235, "sixth")); System.out.println("\n" + hSet); printSet(hSet); // NOTE : although all the above objects are different // but there hashcode could be same --> Lets SEE. // NOTE : all the elements in it are upcasted to Element // --> as to make only one general method :: SO need manual DownCasting now ArrayList<Element> list = setToArrayList(hSet); for (int i = 0; i < list.size(); i++) { Element_NOT_Overridden el = (Element_NOT_Overridden) list.get(i); // NEED DOWNCASTING NOW for (int j = 0; j < list.size(); j++) { Element_NOT_Overridden tempEl = (Element_NOT_Overridden) list.get(j); // NEED DOWNCASTING NOW System.out.print("\n\n" + el + " -vs- " + tempEl); if (el == tempEl) System.out.print("\t--------> NOTICE : same hascode : but then also NOT EQUAL is possible"); System.out.print("\n" + elementToString(el) + " -vs- " + elementToString(tempEl)); if (el.equals(tempEl)) { System.out.println("\t\t\t\t====>>>> EQUAL"); } else { System.out.println("\t\t\t\t====>>>> NOT - EQUAL"); } } System.out.println("\n| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | "); } } public static void test_Element_Overridden() { System.out.println("\n\n\n\n\n\n\t -- TEST : OVERRIDDEN -- \n"); HashSet<Element_Overridden> hSet = new HashSet<>(); hSet.add(new Element_Overridden(1, 23, "first")); hSet.add(new Element_Overridden(2, 62, "second")); hSet.add(new Element_Overridden(3, 25, "third")); // --> NOTE : we have fucked with the intVal parameter --> IMPORTANT CONCEPT hSet.add(new Element_Overridden(1, 53, "fifth")); hSet.add(new Element_Overridden(2, 235, "sixth")); System.out.println("\n" + hSet); printSet(hSet); // NOTE : although all the above objects are different // but there hashcode could be same --> Lets SEE. // NOTE : all the elements in it are upcasted to Element // --> as to make only one general method :: SO need manual DownCasting now ArrayList<Element> list = setToArrayList(hSet); for (int i = 0; i < list.size(); i++) { Element_Overridden el = (Element_Overridden) list.get(i); // NEED DOWNCASTING NOW for (int j = 0; j < list.size(); j++) { Element_Overridden tempEl = (Element_Overridden) list.get(j); // NEED DOWNCASTING NOW System.out.print("\n\n" + el + " -vs- " + tempEl); if (el == tempEl) { System.out.print( " ---> NOTICE : same hascode - so same but see there are obj with same code BUT THEN also NOT EQUAL \n\t\t\t but NOTE: this means the representation ClassName@Code is not actual value of reference : its just representation, \n\t\t\twhich is used when you have not imlemented the toString() method of that class"); } else if(el.hashCode() == tempEl.hashCode()){ System.out.print("\t\t <<--- SEE"); } System.out.print("\n" + elementToString(el) + " -vs- " + elementToString(tempEl)); if (el.equals(tempEl)) { System.out.println("\t\t\t\t====>>>> EQUAL"); } else { System.out.println("\t\t\t\t====>>>> NOT - EQUAL"); } } System.out.println("\n| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | "); } } // NOTE : they both inherit Elements ---> upcasting public static void printSet(Set set) { System.out.print("\n [[[ "); for (Object obj : set) { Element e = (Element) obj;// DOWNCASTING System.out.print(e.intVal + "-" + e.stringVal + "-" + e.doubleVal + " | "); } System.out.println("]]] "); } public static ArrayList<Element> setToArrayList(Set set) { ArrayList<Element> aList = new ArrayList<>(); for (Object o : set) { Element e = (Element) o; // DOWN-CASTING aList.add(e); } return aList; } // upcasting for both the classes : Element_NOT_Overridden && Element_Overridden public static String elementToString(Element e) { return (e.intVal + "-" + e.stringVal + "-" + e.doubleVal + " "); } }
run
|
edit
|
history
|
help
0
classwork
MinMaxArray
Bank System
++a vs a++ and calculations around different variables
forloop
Java Q4
Coloring map
DecoratedFrame.java
test1
Leetcode 937. Reorder Log Files๐ฎ๐ณ๐ฎ๐ณ๐ฎ๐ณ