I am trying to figure out how could I get the top 10 values from the HashMap. I was initially trying to use the TreeMap and have it sort by value and then take the first 10 values however it seems that that is not the option, as TreeMap sorts by key.
I want to still be able to know which keys have the highest values, the K, V of the map are String, Integer.
Maybe you should implement the Comparable Interface to your value objects stored in the hashmap.
Then you can create a array list of all values:
List<YourValueType> l = new ArrayList<YourValueType>(hashmap.values());
Collection.sort(l);
l = l.subList(0,10);
Regards
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
public class Testing {
public static void main(String[] args) {
HashMap<String,Double> map = new HashMap<String,Double>();
ValueComparator bvc = new ValueComparator(map);
TreeMap<String,Double> sorted_map = new TreeMap<String,Double>(bvc);
map.put("A",99.5);
map.put("B",67.4);
map.put("C",67.4);
map.put("D",67.3);
System.out.println("unsorted map: "+map);
sorted_map.putAll(map);
System.out.println("results: "+sorted_map);
}
}
class ValueComparator implements Comparator<String> {
Map<String, Double> base;
public ValueComparator(Map<String, Double> base) {
this.base = base;
}
// Note: this comparator imposes orderings that are inconsistent with equals.
public int compare(String a, String b) {
if (base.get(a) >= base.get(b)) {
return -1;
} else {
return 1;
} // returning 0 would merge keys
}
}
I am afraid you'll have to iterate over the entire map. Heap is a commonly-used data structure for finding top K elements, as explained in this book.
If you are trying to get the 10 highest values of the map (assuming the values are numeric or at least implementing Comparable) then try this:
List list = new ArrayList(hashMap.values());
Collections.sort(list);
for(int i=0; i<10; i++) {
// Deal with your value
}
Let's assume you have a Map, but this example can work for any type of
Map<String, String> m = yourMethodToGetYourMap();
List<String> c = new ArrayList<String>(m.values());
Collections.sort(c);
for(int i=0 ; i< 10; ++i) {
System.out.println(i + " rank is " + c.get(i));
}
I base my answer in this one from sk2212
First you need to implement a descending comparator:
class EntryComparator implements Comparator<Entry<String,Integer>> {
/**
* Implements descending order.
*/
#Override
public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) {
if (o1.getValue() < o2.getValue()) {
return 1;
} else if (o1.getValue() > o2.getValue()) {
return -1;
}
return 0;
}
}
Then you can use it in a method such as this one for the attribute "hashmap":
public List<Entry<String,Integer>> getTopKeysWithOccurences(int top) {
List<Entry<String,Integer>> results = new ArrayList<>(hashmap.entrySet());
Collections.sort(results, new EntryComparator());
return results.subList(0, top);
}
public static void main(String[] args) {
HashMap<String, Integer> map = new HashMap<String, Integer>();
// Initialize map
System.out.println(getTopKeysWithOccurences(map, 10));
}
public static List<Entry<String,Integer>> getTopKeysWithOccurences(Map mp, int top) {
List<Entry<String,Double>> results = new ArrayList<>(mp.entrySet());
Collections.sort(results, (e1,e2) -> e2.getValue() - e1.getValue());
//Ascending order - e1.getValue() - e2.getValue()
//Descending order - e2.getValue() - e1.getValue()
return results.subList(0, top);
}
Related
I'm currently working on an assignement which requires me to use a Treeset to sort pairs of value. I'm asked to use SimpleEntry. I'm storing them on a Treeset :
TreeSet treeSet = new TreeSet(new PairComparator());
SimpleEntry pair = new SimpleEntry(weight,source);
treeSet.add(pair);
Here is my custom Comparator :
static class PairComparator implements Comparator<AbstractMap.SimpleEntry<Integer, Integer>> {
#Override
public int compare(AbstractMap.SimpleEntry<Integer,Integer> o1, AbstractMap.SimpleEntry<Integer,Integer> o2) {
int key1 = o1.getKey() ;
int key2 = o2.getKey();
return key1 - key2;
}
}
I get an error I don't understand, which is :
class java.util.HashMap cannot be cast to class java.lang.Integer
Could you explain me what is happening? I'm not totally familiar with defining a custom comparator.
Thanks for your attention !
EDIT : I was adding a Hashmap as a key on my SimpleEntry and not an integer, which is now quite obvious...
Seems like somehow you have added a HashMap as a key to one of the SimpleEntries in your TreeSet.
Show a complete runnable example please.
This code works for me:
import java.util.AbstractMap;
import java.util.AbstractMap.SimpleEntry;
import java.util.Comparator;
import java.util.TreeSet;
public class Main {
static class PairComparator implements Comparator<SimpleEntry<Integer, Integer>> {
#Override
public int compare(SimpleEntry<Integer, Integer> o1, SimpleEntry<Integer, Integer> o2) {
int key1 = o1.getKey();
int key2 = o2.getKey();
return key1 - key2;
}
}
public static void main(String[] args) {
TreeSet treeSet = new TreeSet(new PairComparator());
treeSet.add(new SimpleEntry(42, 69));
treeSet.add(new SimpleEntry(37, 65));
treeSet.add(new SimpleEntry(23, 19));
treeSet.add(new SimpleEntry(54, 12));
System.out.println(treeSet);
}
}
You didn't specify the types for the TreeSet and some other data structures but I did it here and it works just fine. I did change your comparator. Subtracting integer values to do the comparisons is not a good idea as it can cause problems for large numbers.
TreeSet<SimpleEntry<Integer, Integer>> treeSet =
new TreeSet<>(new PairComparator());
Random r = new Random(23);
for (int i = 0; i < 10; i++) {
int weight = r.nextInt(100);
int source = r.nextInt(100);
SimpleEntry<Integer, Integer> pair =
new SimpleEntry<>(weight, source);
treeSet.add(pair);
}
treeSet.forEach(System.out::println);
Prints the following (duplicate keys ignored)
18=43
22=17
24=30
27=48
81=95
83=47
89=70
90=10
94=87
The comparator
static class PairComparator implements
Comparator<AbstractMap.SimpleEntry<Integer, Integer>> {
public int compare(
AbstractMap.SimpleEntry<Integer, Integer> o1,
AbstractMap.SimpleEntry<Integer, Integer> o2) {
int key1 = o1.getKey();
int key2 = o2.getKey();
return key1 < key2 ? -1 : key1 > key2 ? 1 : 0;
}
}
You can also specify the following as a comparator.
Comparator<AbstractMap.SimpleEntry<Integer,Integer>> pairComparator =
(se1,se2)-> Integer.compare(se1.getKey(),se2.getKey());
I'm trying to sort a HashMap<String, Long>. I'm have the following code for sorting:
private static class ValueComparator implements Comparator<String>{
HashMap<String, Long> map = new HashMap<String, Long>();
public ValueComparator(HashMap<String, Long> map){
this.map.putAll(map);
}
#Override
public int compare(String s1, String s2) {
if(map.get(s1) > map.get(s2)){
System.out.println("s1: " + s1 + "; s2: " + s2);
return -1;
}
else if (map.get(s1).equals(map.get(s2))) {
return 0;
}
else{
return 1;
}
}
}
private static TreeMap<String, Long> sortMapByValue(HashMap<String, Long> map){
Comparator<String> comparator = new ValueComparator(map);
//TreeMap is a map sorted by its keys.
//The comparator is used to sort the TreeMap by keys.
TreeMap<String, Long> result = new TreeMap<String, Long>(comparator);
result.putAll(map);
System.out.println("DONE sort");
return result;
}
The problem is, when several different keys have the same values, only one of the key makes it into the final map:
EXAMPLE:
public class Test {
public static void main(String[] args) {
HashMap<String, Long> hashMap = new HashMap<>();
hashMap.put("Cat", (long) 4);
hashMap.put("Human", (long) 2);
hashMap.put("Dog", (long) 4);
hashMap.put("Fish", (long) 0);
hashMap.put("Tree", (long) 1);
hashMap.put("Three-legged-human", (long) 3);
hashMap.put("Monkey", (long) 2);
System.out.println(hashMap); //7 pairs
System.out.println(sortMapByValue(hashMap)); //5 pairs
}
}
How would I fix it?
I don't think it's fixable you are using the the maps in an unintended way and breaking contracts. Tree map is expecting to be sorted by the key and the key is expected to be unique so when the compare == 0 it will just override the node's value. You can always implement your own TreeMap and make it do whatever you want it to.
I'm not sure what you want to do with it but I think you need something like
TreeMap<Long,List<String>>
http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/687fd7c7986d/src/share/classes/java/util/TreeMap.java
if (cpr != null) {
do {
parent = t;
cmp = cpr.compare(key, t.key);
if (cmp < 0)
t = t.left;
else if (cmp > 0)
t = t.right;
else
return t.setValue(value);
} while (t != null);
I have a map of string keys and int values, I to sort them, and keep them sorted when I change values.
I tried using a treemap for the sorted pairs and a normal map for the unsorted paris so I can use it in the comparator, but after a one value passes the other I get a null expection, this is the defenition:
public static TreeMap<String, Long> countryData;
public static ValueComparator bvc;
public static void setCountryData(HashMap<String, Long> map){
bvc = new ValueComparator(map);
countryData = new TreeMap<String, Long>(bvc);
countryData.putAll(map);
System.out.println(Arrays.toString(countyNames));
System.out.println(countryData.values());
}
public static class ValueComparator implements Comparator<String> {
Map<String, Long> base;
public ValueComparator(Map<String, Long> base) {
this.base = base;
}
public int compare(String a, String b) {
if(base.get(a).equals(base.get(b))){
return 0;
}
if (base.get(a) > base.get(b)) {
return -1;
} else {
return 1;
}
}
}
this is how I change the values:
General.bvc.base.put(country, newValue);
General.countryData.put(country, newValue);
after one value passes another and I try to acces it, I get a null, how can I Do this?
How can I have a TreeMap which is sorted by value ..
You can't. They are sorted by key.
...
Irrelevant.
In the code I have posted below, I need to remove the duplicates from the HashMap (the highest alphabetical value gets to stay in the map) and print the keys of the k highest values after the duplicates are removed. How do I do this? I tried with a HashSet but I am pretty clueless.
public ArrayList<String> mostOften(int k)
{
ArrayList<String> lista = new ArrayList<String>();
HashMap<String,Integer> temp = new HashMap<String, Integer>();
for(String it : wordList)
{
if(temp.containsKey(it))
temp.put(it, temp.get(it)+1);
else
temp.put(it, 1);
}
temp = sortByValues(temp);
Set<Integer> set = new HashSet<Integer>(temp.values());
System.out.println(set);
return lista;
}
private static HashMap sortByValues(HashMap map)
{
List list = new LinkedList(map.entrySet());
Collections.sort(list, new Comparator()
{
public int compare(Object o1, Object o2)
{
return ((Comparable)((Map.Entry) (o1)).getValue()).compareTo(((Map.Entry) (o2)).getValue());
}
});
HashMap sortedHashMap = new LinkedHashMap();
for (Iterator it = list.iterator(); it.hasNext();)
{
Map.Entry entry = (Map.Entry) it.next();
sortedHashMap.put(entry.getKey(), entry.getValue());
}
return sortedHashMap;
}
If you are trying to do a frequency count of words you are heading down the wrong road. Java 8 does this much easier and cleaner.
You need these imports
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
The code
public static void main(String[] args) {
printTopWords(Arrays.asList("Hello World Hello , Bye World".split(" ")), 2);
}
public static void printTopWords(List<String> words, int limit) {
// using the Stream API
words.stream()
// create a map of words with the count of those words
.collect(Collectors.groupingBy(w -> w, Collectors.counting()))
// take that map as a stream of entries
.entrySet().stream()
// sort them by count in reverse order
.sorted(Comparator.comparing(Map.Entry<String, Long>::getValue).reversed())
// limit the number to get top Strings
.limit(limit)
// keep just the key ie. drop the count.
.map(Map.Entry::getKey)
// print them
.forEach(System.out::println);
}
prints
Hello
World
If you are not familiar with java 8 streams and lambdas then below answer would be helpful to you :
public class Java7Way {
public static void main(String[] args) {
Map<String, Integer> myMap = new HashMap<>();
myMap.put("A", 20);
myMap.put("A", 38);
myMap.put("B", 40);
myMap.put("K", 23);
System.out.println(sortByValue(myMap,2).toString());
}
public static <K, V extends Comparable<? super V>> Map<K, V>
sortByValue(Map<K, V> map,int limit) {
List<Map.Entry<K, V>> list
= new LinkedList<>(map.entrySet());
Collections.sort(list, new Comparator<Map.Entry<K, V>>() {
#Override
public int compare(Map.Entry<K, V> o1, Map.Entry<K, V> o2) {
return (o1.getValue()).compareTo(o2.getValue());
}
}
.reversed()//to arrange it in decending order
);
Map<K, V> result = new LinkedHashMap<>();//maintains the order which the entries were put into the map
for (Map.Entry<K, V> entry : list) {
if (limit == 0) {
break;
}
result.put(entry.getKey(), entry.getValue());
limit--;
}
return result;
}
}
Out-put :
{B=40, A=38}
I recommend using TreeBidiMap from Apache Commons Collection. In this structure all keys and all values sorted according to the natural order for the key's and value's classes.
For your code:
BidiMap<String,Integer> temp = new TreeBidiMap<String, Integer>();
for(String it : wordList)
{
if(temp.containsKey(it))
temp.put(it, temp.get(it)+1);
else
temp.put(it, 1);
}
// print values unsing natural sorting in reverse order
BidiMap inverse = temp.inverseBidiMap();
for (MapIterator it = inverse.mapIterator(); it.hasPrevious();) {
String k = it.next();
Integer s = it.getValue();
System.out.printLn(s + " = " + k);
}
I have a Hashmap that links a zipcodes stored as keys and population stored as values in a hashmap.
The hashmap contains around 33k entries.
I'm trying to get the 5 highest population values from 5 zip codes and print out the 5 zip codes ASSOCIATED with the 5 highest population, but I'm having trouble understanding the algorithm of how to do it.
If it was just one, its easy but the 5 restriction is giving me some trouble.
I know to store the 5 values in an int array and I have a counter to determine when 5 of them are stored, but thats it.
Thanks
int populatedCounter = 0;
int[] populatedZip = new int[5];
it = zipCodePop.entrySet().iterator();
while (it.hasNext())
{
Map.Entry pairs = (Map.Entry)it.next();
for (int i = 0; i < populatedZip.length; i++)
{
}
}
}
Putting the entries of such a set into a list and sorting it is one option. But 33k elements is a number where the O(n*log(n)) complexity of sorting might already have a noticable performance impact.
One apporach would be to employ the PriorityQueue that nr4bt already mentioned (I wrote this snippet while he answered). It basically inserts all elements into a PriorityQueue that is sorted according to the values of the map entries.
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.PriorityQueue;
public class GreatestOfMap
{
public static void main(String[] args)
{
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("zip000", 1234);
map.put("zip001", 2345);
map.put("zip002", 3456);
map.put("zip003", 4567);
map.put("zip004", 5678);
map.put("zip005", 6789);
map.put("zip006", 123);
map.put("zip007", 234);
map.put("zip008", 456);
map.put("zip009", 567);
map.put("zip010", 7890);
map.put("zip011", 678);
map.put("zip012", 789);
map.put("zip013", 890);
int n = 5;
List<Entry<String, Integer>> greatest = findGreatest(map, 5);
System.out.println("Top "+n+" entries:");
for (Entry<String, Integer> entry : greatest)
{
System.out.println(entry);
}
}
private static <K, V extends Comparable<? super V>> List<Entry<K, V>>
findGreatest(Map<K, V> map, int n)
{
Comparator<? super Entry<K, V>> comparator =
new Comparator<Entry<K, V>>()
{
#Override
public int compare(Entry<K, V> e0, Entry<K, V> e1)
{
V v0 = e0.getValue();
V v1 = e1.getValue();
return v0.compareTo(v1);
}
};
PriorityQueue<Entry<K, V>> highest =
new PriorityQueue<Entry<K,V>>(n, comparator);
for (Entry<K, V> entry : map.entrySet())
{
highest.offer(entry);
while (highest.size() > n)
{
highest.poll();
}
}
List<Entry<K, V>> result = new ArrayList<Map.Entry<K,V>>();
while (highest.size() > 0)
{
result.add(highest.poll());
}
return result;
}
}
Try this, using standard methods and assuming that the population count is stored as Integers in the HashMap:
List<Integer> list = new ArrayList<Integer>(zipCodePop.values());
Collections.sort(list, Collections.reverseOrder());
List<Integer> top5 = list.subList(0, 5);
public class CheckHighiestValue {
public static void main(String... s) {
HashMap<String, Integer> map = new HashMap<String, Integer>();
map.put("first", 10000);
map.put("second", 20000);
map.put("third", 300);
map.put("fourth", 800012);
map.put("fifth", 5000);
map.put("sixth", 30012);
map.put("seventh", 1234);
map.put("eighth", 45321);
map.put("nineth", 5678);
Set<Entry<String, Integer>> set = map.entrySet();
List<Entry<String, Integer>> list = new ArrayList<Entry<String, Integer>>(
set);
Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {
#Override
public int compare(Entry<String, Integer> o1,
Entry<String, Integer> o2) {
return o2.getValue().compareTo(o1.getValue());
}
});
System.out.println(list.subList(0, 5));
}
}
PriorityQueue would help too, and also a nice topic about how to get top k from a list, you can check this link
PriorityQueue<Integer> p = new PriorityQueue<Integer>(5);
int[] a = new int[]{3,5,10,1,23,42,66,1333,545,110};
for (int i : a){
p.add(i);
if (p.size() > 5){
p.poll();
}
}
//output will be highest 5, [42, 66, 110, 1333, 545]
You can have O(n log(k)) time complexity // k is your top value count.
This is something i made and hopefully provides you something that you want to use.
public class TopsCollection {
private static Map<String, Integer> collectors = new HashMap<>();
public TopsCollection() {
}
public void add(String playerName, int score) {
collectors.put(playerName, score);
}
public void clearCollectors() {
synchronized (collectors) {
collectors.clear();
}
}
public List<Map.Entry<String, Integer>> getTops() {
return collectors.entrySet().stream().sorted(comparing(Map.Entry::getValue, reverseOrder())).limit(5).collect(toList());
}
public int getTopByName(String name) {
for (int i = 0; i < getTops().size(); i++) {
if (getTops().get(i).getKey().contains(name)) {
return i;
}
}
return 0;
}
getTopByName allows you to get the top place of the specified name.
How would you do this without a computer, with just a piece of paper and a pencil? Pretend you had a stack of index cards that had numbers on them, and it was your job to find the 5 highest numbers. How would you do that? Write down steps that somebody else could follow to achieve the goal, and when you have those steps written out, you'll have an algorithm that you can start thinking about implementing with code.
You say that a single maximum is easy, so do it exactly like you would with a single maximum, but keep track of the five maximums instead. An array of maximums might be helpful here.
Using Streams
int[] populatedZip = map.entrySet().parallelStream()
.sorted(Map.Entry.<String, Integer>comparingByValue())
.limit(5)
.mapToInt(entry -> entry.getValue())
.toArray();