I have a HashMap
defined like this...
HashMap uniqueNames = new HashMap();
It stor
If you only want the value you can go for this. In this example I had to get the maximum frequency of a number among an array of 'n' numbers
{
int n = sc.nextInt();
int arr[] = new int[n];
int freq = 1;
int i;
Map<Integer,Integer> myMap = new HashMap<Integer,Integer>();
for(i=0;i<n;i++){
arr[i] = sc.nextInt();
if(!myMap.containsKey(arr[i])){
myMap.put(arr[i],freq);
}
else
{
myMap.put(arr[i],(myMap.get(arr[i])+1));
}
}
int max = 0;
for(i=0;i<n;i++){
if(myMap.get(arr[i])>max)
max = myMap.get(arr[i]);
}
System.out.println(max);
}
List<String> list= new ArrayList<String>();
HashMap<String, Integer> map=new HashMap<String,Integer>();
for(String string: list)
{
if(map.containsKey(string))
{
map.put(string, map.get(string)+1);
}
else {
map.put(string, 1);
}
}
Entry<String,Integer> maxEntry = null;
for(Entry<String,Integer> entry : map.entrySet()) {
if (maxEntry == null || entry.getValue() > maxEntry.getValue()) {
maxEntry = entry;
}
}
1.Try this it may help.
static <K, V> List<K> getAllKeysForValue(Map<K, V> mapOfWords, V value)
{
List<K> listOfKeys = null;
//Check if Map contains the given value
if(mapOfWords.containsValue(value))
{
// Create an Empty List
listOfKeys = new ArrayList<>();
// Iterate over each entry of map using entrySet
for (Map.Entry<K, V> entry : mapOfWords.entrySet())
{
// Check if value matches with given value
if (entry.getValue().equals(value))
{
// Store the key from entry to the list
listOfKeys.add(entry.getKey());
}
}
}
// Return the list of keys whose value matches with given value.
return listOfKeys;
}
There are two ways of going about this actually.
If you are going to be doing this frequently, I would actually suggest storing the mapping in reverse, where the key is the number of times a name has appeared, and the value is a list of names which appeared that many times. I would also use a HashMap to perform the lookups in the other direction as well.
TreeMap <Integer, ArrayList <String>> sortedOccurrenceMap =
new TreeMap <Integer, ArrayList <String>> ();
HashMap <String, Integer> lastNames = new HashMap <String, Integer> ();
boolean insertIntoMap(String key) {
if (lastNames.containsKey(key)) {
int count = lastNames.get(key);
lastNames.put(key, count + 1);
//definitely in the other map
ArrayList <String> names = sortedOccurrenceMap.get(count);
names.remove(key);
if(!sortedOccurrenceMap.contains(count+1))
sortedOccurrenceMap.put(count+1, new ArrayList<String>());
sortedOccurrenceMap.get(count+1).add(key);
}
else {
lastNames.put(key, 1);
if(!sortedOccurrenceMap.contains(1))
sortedOccurrenceMap.put(1, new ArrayList<String>());
sortedOccurrenceMap.get(1).add(key);
}
}
Something similar for deleting...
And finally, for your search:
ArrayList <String> maxOccurrences() {
return sortedOccurrenceMap.pollLastEntry().getValue();
}
Returns the List of names that have the max occurrences.
If you do it this way, the searching can be done in O(log n) but the space requirements increase (only by a constant factor though).
If space is an issue, or performance isn't a problem, simply iterate through the uniqueNames.keySet and keep track of the max.
If you have to use a HashMap, then the simplest way is probabably just to loop through the Map looking for the maximum
Entry<String,Integer> maxEntry = null;
for(Entry<String,Integer> entry : uniqueNames.entrySet()) {
if (maxEntry == null || entry.getValue() > maxEntry.getValue()) {
maxEntry = entry;
}
}
// maxEntry should now contain the maximum,
Seems like you want something a bit like a SortedMap
but one sorted on the value, not the key. I don't think such a thing exists in the standard API.
It might be better to create a Frequency class and store instances in a SortedSet
instead.
import java.util.Set;
import java.util.TreeSet;
public class Frequency implements Comparable<Frequency> {
private String name;
private int freq;
public Frequency(String name, int freq) {
this.name = name;
this.freq = freq;
}
public static void main(String[] args) {
Set<Frequency> set = new TreeSet<Frequency>();
set.add(new Frequency("fred", 1));
set.add(new Frequency("bob", 5));
set.add(new Frequency("jim", 10));
set.add(new Frequency("bert", 4));
set.add(new Frequency("art", 3));
set.add(new Frequency("homer", 5));
for (Frequency f : set) {
System.out.println(f);
}
}
@Override
public boolean equals(Object o) {
if (o == null) return false;
if (o.getClass().isAssignableFrom(Frequency.class)) {
Frequency other = (Frequency)o;
return other.freq == this.freq && other.name.equals(this.name);
} else {
return false;
}
}
@Override
public int compareTo(Frequency other) {
if (freq == other.freq) {
return name.compareTo(other.name);
} else {
return freq - other.freq;
}
}
@Override
public String toString() {
return name + ":" + freq;
}
}
Output:
fred:1
art:3
bert:4
bob:5
homer:5
jim:10