Skip to content

Commit 21b0110

Browse files
committed
Refactor
1 parent b1b84f4 commit 21b0110

2 files changed

Lines changed: 6 additions & 8 deletions

File tree

src/main/java/com/ab/collection/maps/HashCodeEqualsContract.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
*
1212
* Contract:
1313
* If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
14-
*
1514
* It is not required that if two objects are unequal according to the equals method, then calling the hashCode method on each of the two objects must produce distinct integer results.
1615
* However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
1716
*/
@@ -63,17 +62,16 @@ public static void main(String[] args) {
6362
map.put(p2,2345);
6463
map.put(p3,3456);
6564

66-
//implement only equals
65+
//if you implement only equals
6766
//default hasCode() impl returns distinct integer for distinct object
6867
//you will not able to retrieve what you have added as the logical equal products have different hashcode
6968
Product p4 = new Product("P2",24);
7069
System.out.println(map.get(p4));
7170

72-
//implement only hashcode
71+
//if you implement only hashcode
7372
//default equals() impl returns true only if two objects had the same reference pointer
7473
//you can add duplicate keys which are logically equal
75-
//also you will not able to retrieve what you have added as the logical equal products have different hashcode
76-
Product p5 = new Product("P3",29);
74+
Product p5 = new Product("P3", 29);
7775
map.put(p5,4567);
7876

7977
for (Map.Entry<Product,Integer> e:

src/main/java/com/ab/collection/sort/ComparatorDemo.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public static void main(String[] args) {
2626
Utils.printArray(workerArr);
2727

2828
//can pass on the fly anonymous comparator
29-
Arrays.sort(workerArr, new Comparator<Worker>() {
29+
Arrays.sort(workerArr, new Comparator<>() {
3030
@Override
3131
public int compare(Worker o1, Worker o2) {
3232
return (int) (o1.getSalary() - o2.getSalary());
@@ -38,11 +38,11 @@ public int compare(Worker o1, Worker o2) {
3838
//Sort on multiple fields – Group by sort
3939
//Method 1
4040
//compare two worker object first on their id and if they are same then on the name.
41-
Arrays.sort(workerArr, new Comparator<Worker>() {
41+
Arrays.sort(workerArr, new Comparator<>() {
4242
@Override
4343
public int compare(Worker o1, Worker o2) {
4444
int flag = o1.getId() - o2.getId();
45-
if(flag==0) flag = o1.getFirstName().compareTo(o2.getFirstName());
45+
if (flag == 0) flag = o1.getFirstName().compareTo(o2.getFirstName());
4646
return flag;
4747
}
4848
});

0 commit comments

Comments
 (0)