forked from roytuts/java
-
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
Showing
4 changed files
with
118 additions
and
0 deletions.
There are no files selected for viewing
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,13 @@ | ||
plugins { | ||
id 'java-library' | ||
} | ||
|
||
sourceCompatibility = 12 | ||
targetCompatibility = 12 | ||
|
||
repositories { | ||
jcenter() | ||
} | ||
|
||
dependencies { | ||
} |
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 @@ | ||
You can read tutorial https://www.roytuts.com/using-comparator-in-hashmap/ |
57 changes: 57 additions & 0 deletions
57
java-comparator-in-hashmap/src/main/java/com/roytuts/java/comparator/in/hashmap/Book.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,57 @@ | ||
package com.roytuts.java.comparator.in.hashmap; | ||
|
||
public class Book { | ||
|
||
private String title; | ||
private String author; | ||
|
||
public Book(String title, String author) { | ||
this.title = title; | ||
this.author = author; | ||
} | ||
|
||
public String getTitle() { | ||
return title; | ||
} | ||
|
||
public String getAuthor() { | ||
return author; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
final int prime = 31; | ||
int result = 1; | ||
result = prime * result + ((author == null) ? 0 : author.hashCode()); | ||
result = prime * result + ((title == null) ? 0 : title.hashCode()); | ||
return result; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (this == obj) | ||
return true; | ||
if (obj == null) | ||
return false; | ||
if (getClass() != obj.getClass()) | ||
return false; | ||
Book other = (Book) obj; | ||
if (author == null) { | ||
if (other.author != null) | ||
return false; | ||
} else if (!author.equals(other.author)) | ||
return false; | ||
if (title == null) { | ||
if (other.title != null) | ||
return false; | ||
} else if (!title.equals(other.title)) | ||
return false; | ||
return true; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Book [title=" + title + ", author=" + author + "]"; | ||
} | ||
|
||
} |
47 changes: 47 additions & 0 deletions
47
...or-in-hashmap/src/main/java/com/roytuts/java/comparator/in/hashmap/HashMapComparator.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,47 @@ | ||
package com.roytuts.java.comparator.in.hashmap; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.Comparator; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Map.Entry; | ||
import java.util.Set; | ||
|
||
public class HashMapComparator { | ||
|
||
public static void main(String[] args) { | ||
Map<Book, String> bookMap = new HashMap<>(); | ||
|
||
bookMap.put(new Book("Java", "James Gosling"), "Java"); | ||
bookMap.put(new Book("C++", "Bjourn Stroustup"), "C++"); | ||
bookMap.put(new Book("C", "Denish Ritche"), "C"); | ||
bookMap.put(new Book("Databse", "C J Date"), "Database"); | ||
|
||
System.out.println("Unsorted HashMap Values"); | ||
System.out.println("-----------------------"); | ||
|
||
bookMap.forEach((k, v) -> System.out.println(k + " => " + v)); | ||
|
||
System.out.println(); | ||
|
||
Set<Entry<Book, String>> set = bookMap.entrySet(); | ||
List<Entry<Book, String>> list = new ArrayList<Entry<Book, String>>(set); | ||
|
||
//Collections.sort(list, (b1, b2) -> (b1.getValue()).compareTo(b2.getValue())); | ||
|
||
Collections.sort(list, new Comparator<Map.Entry<Book, String>>() { | ||
@Override | ||
public int compare(Entry<Book, String> b1, Entry<Book, String> b2) { | ||
return (b1.getValue()).compareTo(b2.getValue()); | ||
} | ||
}); | ||
|
||
System.out.println("Sorted HashMap Values"); | ||
System.out.println("---------------------"); | ||
|
||
list.forEach(item -> System.out.println(item.getKey() + " => " + item.getValue())); | ||
} | ||
|
||
} |