Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
roytuts authored Sep 20, 2019
1 parent 656ab77 commit 10c5aae
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 0 deletions.
13 changes: 13 additions & 0 deletions java-comparator-in-hashmap/build.gradle
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 {
}
1 change: 1 addition & 0 deletions java-comparator-in-hashmap/readme.rst
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/
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 + "]";
}

}
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()));
}

}

0 comments on commit 10c5aae

Please sign in to comment.