-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8bbcc04
commit ceab87d
Showing
3 changed files
with
70 additions
and
0 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
JavaDsaWithTest/src/main/java/org/practice/dsa/collections/ComparableDemo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package org.practice.dsa.collections; | ||
|
||
public class ComparableDemo implements Comparable<ComparableDemo> { | ||
private int id; | ||
private String name; | ||
|
||
public ComparableDemo(int id, String name) { | ||
this.id = id; | ||
this.name = name; | ||
} | ||
|
||
public int getId() { | ||
return id; | ||
} | ||
|
||
public void setId(int id) { | ||
this.id = id; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
@Override | ||
public int compareTo(ComparableDemo o) { | ||
// if (this.id > o.id) { | ||
// return 1; | ||
// } | ||
// if (this.id < o.id) { | ||
// return -1; | ||
// } | ||
// return 0; | ||
|
||
return Integer.compare(this.id, o.id); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Student{id=" + id + ", name='" + name + "'}"; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
JavaDsaWithTest/src/main/java/org/practice/dsa/collections/ComparableExample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package org.practice.dsa.collections; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
public class ComparableExample { | ||
public static void main(String[] args) { | ||
List<ComparableDemo> demo = new ArrayList<>(); | ||
demo.add(new ComparableDemo(3, "V")); | ||
demo.add(new ComparableDemo(1, "B")); | ||
demo.add(new ComparableDemo(2, "X")); | ||
|
||
Collections.sort(demo); | ||
demo.forEach(System.out::println); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
...aWithTest/src/main/java/org/practice/dsa/collections/ComparatorAndComparable.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
Both Comparator and Comparable are used to save the custom sorting logic in Java. | ||
|
||
## Comparable: | ||
- Purpose: To define the natural ordering of objects of a class. | ||
- Interface: Found in `java.lang`. | ||
- Method: `int compareTo(T o);` | ||
- Modifies Class: The class itself implement `Comparable` interface. |